FelixXu
FelixXu

Reputation: 3

Java Exception handle case

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

Upvotes: 0

Views: 298

Answers (5)

Stephen C
Stephen C

Reputation: 718708

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.

Upvotes: 0

Rose
Rose

Reputation: 423

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

Upvotes: 1

drew moore
drew moore

Reputation: 32670

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
}

Upvotes: 0

Rawa
Rawa

Reputation: 13906

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.
}

Upvotes: 0

jmduke
jmduke

Reputation: 1657

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

Upvotes: 1

Related Questions