Vikram
Vikram

Reputation: 4106

Alternatives to catching NullPointerException

I recently got to know that using try and catch blocks for NullPointerExceptions is a bad practice.

If so, then I have following questions:

  1. Why is this a bad practice?
  2. What are the alternatives to catching a NullPointerException?

Upvotes: 3

Views: 1222

Answers (5)

user3016087
user3016087

Reputation: 69

either wrapped illegal argument exception when you are at transliterate service side , or just handled predictable null case to work on preconditions of a model or method, or catch exception , inside this catch, if instance Of ... to write log or more. generally, never directly catch null point exception, it is run time exception, it has logic: if you know where will be null in your code , then handle it. if you don't know, the, after you catch , what you do? you are not JVM , right?

Upvotes: -1

Philipp
Philipp

Reputation: 69663

When you add a try/catch block around a section of code which you expect to throw a NPE under specific circumstances, you might inadvertently forget about another object used by that code which might also be null and cause one. When you then add the code to react on the expected NPE in the catch block, the code will also be executed on the unexpected NPE. This will output a misleading error message at best and result in completely unexpected program behavior at worst.

An example:

try {
    ThingamabobFactory.getInstance().createThingamabob(ThingamabobFactory.getDefaults()).thingamabobify();
} catch(NullPointerException e) {
      // so... which of the above method calls did return null?
      // ...or was the NPE even thrown INSIDE one of these methods??
}

So when you expect that an object will be null in some situations and you want to handle this case, do so by checking if (object == null) to avoid any false-positives.

Upvotes: 8

Mihai8
Mihai8

Reputation: 3147

You can avoid to treated directly by using throws, or you can try an alternative method by using BCEL.

Upvotes: 0

Patricia Shanahan
Patricia Shanahan

Reputation: 26185

The big problem with catching NullPointerException is knowing what to do about it, because it can have so many possible causes.

You can be more sure you know what was null and why if you specifically test for null. For example, suppose a block of code contains a call to a method that returns null under specified circumstances, and you want to take some action if it does so. If you test the method result immediately, you know the null is due to the circumstances that cause the method to return null.

On the other hand, if you wrap the block in a try-catch and catch the NullPointerException, maybe the method returned null, but you cannot be sure that is what happened. Something else may have gone wrong in the try block, causing the exception.

Upvotes: 4

Puppet Master 3010
Puppet Master 3010

Reputation: 261

You should avoid having any exceptions if possible since producing them and sending them through app is expensive in resources.

null point is easy to handle , just check to see if something is null or not

Upvotes: 0

Related Questions