0
public void backendExecute(Map appParams, BackendTaskMetaData metaData) throws Throwable {
    try {
        PeriodicTaskData ptd = (PeriodicTaskData) appParams.get(PeriodicTaskData.PARAM_KEY);
        String bizKey = ptd.getBusinessKey();
    } catch (Exception e) {
        LogServices.app.error("RPTPeriodicReportGenTask:"+ e.getMessage());
    }
}

With regards to the method above, if object pointed to is null, would come across as NullPointerException, I want to know if this exception would be caught or thrown to the invoker method? thanks

TJ-
  • 14,085
  • 12
  • 59
  • 90
FelixXu
  • 3
  • 2

5 Answers5

1

Exception is a parent class of NullPointerException, so it will catch it and not throw it to the calling method.

jmduke
  • 1,657
  • 1
  • 13
  • 11
1

As you are catching Exception class and NullPointerException is its subclass , exception will get catched not throwed.

Rose
  • 403
  • 3
  • 8
0
catch (Exception e)

means that this will catch any exception (i.e. any subclass of Exception) thrown inside the preceding try block - which, yes, includes NullPointerException. Note that this is generally considered a bad practice, as you almost always will want to handle different sorts of exceptions in different ways, hence the need for multiple catch statements.

For instance, consider a method that could potentially throw an IllegalAccessException at compile time or a NullPointerException at runtime - it's difficult to imagine a situation where you'd want to handle them the same way, so you'll typically want to do something like this:

try {
    PeriodicTaskData ptd = (PeriodicTaskData) appParams.get(PeriodicTaskData.PARAM_KEY);
    String bizKey = ptd.getBusinessKey();
} catch (NullPointerException e) {
    //do something
} catch (IllegalAccessException e) { //for example... 
    //do something different
}
drew moore
  • 31,565
  • 17
  • 75
  • 112
0

NullPointerException is a subclass of Exception and thus will be catched, however it is recommended that you don't try and catch runtime exceptions. It is better to avoid them.

For example a null pointer could be avoided by doing the following:

if(ptd != null) {
    ptd.getBusinessKey();
} else {
    //Notify the user in some way or do something else.
}
Rawa
  • 13,357
  • 6
  • 39
  • 55
0

Regard to above method, if object ptd is null, would come across nullpointexception,

Yes.

i want to know this exception would be catch or throw it to invoker method?

The exception would be caught by the handler. The handler will catch Exception and any exception that is descended from it. NullPointerException is a subclass of RuntimeException which is (in turn) a subclass of Exception. Therefore, it will be caught.


Now, if this may be just an illustrative example ... but it is a bad idea to:

  • declare a method as throws Throwable, or
  • catch Exception ... unless you are about to terminate the application.

Declaring a method as throwing Throwable makes it next to impossible for the caller to know what exceptions could be thrown. Instead, the compiler will insist that the caller catches ... or propagates Throwable.

Catching Exception has the problem that you will catch every subtype of Exception ... including various unchecked exceptions that 1) you are not expecting, and 2) are probably symptoms of bugs that you cannot safely recover from.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • now code change to following, add a method invoke before ptd.getBusinessKey, if method anotherMethod meet runtime exception (null pointer or classcast), the original method would catch this exception or throw it to caller?public void backendExecute(Map appParams, BackendTaskMetaData metaData) throws Throwable { try { PeriodicTaskData ptd = (PeriodicTaskData) appParams.get(PeriodicTaskData.PARAM_KEY); this.anotherMethod(); String bizKey = ptd.getBusinessKey(); } catch (Exception e) { LogServices.app.error("RPTPeriodicReportGenTask:"+ e.getMessage()); }} – FelixXu Aug 13 '14 at 04:50
  • Same as before. If the code throws and NPE, the handler will catch it. The only possible difference is that the new call *might* throw an exception. If that happened, you wouldn't *attempt* to call the `getBusinessKey` method. – Stephen C Aug 13 '14 at 05:00