0

If I add this try/catch statement in my code:

try {
   ...
   method1();

} catch (Exception e) {
   call_method();

}

am I invoking "call_method()" for any type of exception, at any level such as java.lang.NullPointerException inside method1() .. right ?

thanks

Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
user680406
  • 5,637
  • 6
  • 24
  • 19
  • One thing to watch out for though... if an exception is thrown in method1 and handled either in that method or somewhere before the exception is thrown to your initial call it won't reach call_method(). Happened to a buddy of mine so I thought I should mention it. – Shaded Apr 08 '11 at 13:40

7 Answers7

3

am I invoking "call_method()" for any type of exception, at any level such as java.lang.NullPointerException inside method1() .. right ?

Yes........

Harry Joy
  • 58,650
  • 30
  • 162
  • 207
1

Yes, that will catch anything that extends Exception. Note especially that this include RuntimeException and its many subclasses (such as NullPointerException as you mention).

It will not catch other Throwable objects, i.e. Error and its many subclasses.

You should be careful not to write overly-broad exception handlers that will end up concealing important problems. (Whether that's the case in this example, I can't say.)

Dave Costa
  • 47,262
  • 8
  • 56
  • 72
0

That would be a steady: Yes! :-D

Mark Mooibroek
  • 7,636
  • 3
  • 32
  • 53
0

Yes, that's correct. However, the catch block will not trigger for Errors, such as an OutOfMemoryError. Which is generally what you want since Errors are supposed to signal fundamental problems an app cannot recover from.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
0

Since you are catching the generic Exception, you will be calling call_method() every time an exception is thrown.

You can see here:

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Exception.html

That exception is the top level class for the exceptions, in which everything eventually inherits from.

However as a rule of thumb, you generally do not want to catch Exception, rather you want to catch specific errors that are pertaining to your try, catch block.

Mike Lewis
  • 63,433
  • 20
  • 141
  • 111
0

Yes - for anything that inherits from Exception. Although in general, unless that method is doing something to handle that exception, it's not typically considered "good form" (but I don't try to assume a person's intent).

You may want to look at Throwable as well: here is a good discussion to view: When should Throwable be used instead of new Exception?

Community
  • 1
  • 1
Alexx
  • 3,572
  • 6
  • 32
  • 39
0

For any exception thrown inside the try block, you will be calling the call_method().

It is because you are trying to catch generic Exception object and not some specific object such as NullPointerException.

If you try something like this,

try { 
  // ....
  method1();
 }
catch(NullPointerException e) {
  // ....
  call_method();
 }  

then only when you have a NullPointerException inside the try block, your catch will catch it & your call_method() will be executed.

Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164