Nice example for a checked unchecked exception


Today I found a nice example for a „checked null pointer exception“ in Ed burns blog.

@Override
public void encodeAll(FacesContext context) throws IOException {
   ResponseWriter writer = context.getResponseWriter();
   String viewName, libraryName, resourceName;
   try {
     viewName = this.getAttributes().get("viewName").toString();
     libraryName = this.getAttributes().get("libraryName").toString();
     resourceName = this.getAttributes().get("resourceName").toString();
    } catch (NullPointerException e) {
     throw new FacesException("Must supply viewName, libraryName and resourceName attributes");
   }
   ...
}

I have always tried to avoid NPE’s with code like this:

if (foobar == null) {
  throw new IllegalArgumentException("foobar must not be null").
}

But from now I will try to use the „checked“ version of a npe check above more often.

Read more about checked / unchecked exceptions here: