aspose file tools*
The moose likes Beginning Java and the fly likes catch(Throwable t) ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "catch(Throwable t) ??" Watch "catch(Throwable t) ??" New topic
Author

catch(Throwable t) ??

sudharshan tettu
Ranch Hand

Joined: Jul 17, 2006
Posts: 124
Hi

What is use of catch(Thowable t) over catch(Exception ex)

I which case that one is used

Thanks
Sudharsan
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Hi,

Throwable is used to catch everything which can be thrown ( i.e. Errors and Exception ).

For more information, please read the API Documentation.

Regards ...


Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by sudarsan tettu:

What is use of catch(Thowable t) over catch(Exception ex)


catch(Exception e) : catches both runtime and checked exceptions.In ideal programs runtime exceptions are not to be caught.Excluding special conditions like in case of a B2B application where the client in expecting something which would suggest that a exceptions has taken place (like error code ), but actual exception might not be making sense for the client.So in those cases you might want to catch the exception and send error code.

Another conditions is when you want your program to continue even after a Fatal blow.In that case also catching runtime exceptions might help.


catch(Throwable t) : catched runtime , checked exceptions and errors too.


Rahul Bhattacharjee
LinkedIn - Blog
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
The difference is that catch(Exception e) is something you are supposed to do and catch(Throwable t) is something you aren't.

Not a good idea to catch a Throwable because:-
  • You might miss a ThreadDeath which ought to be re-thrown, and
  • You might be overlooking an Error which makes it impossible for your application to run properly anyway.

  • Actually catch(Exception e) is not really a good idea either; you are better off catching subclasses of Exception.
    Rahul Bhattacharjee
    Ranch Hand

    Joined: Nov 29, 2005
    Posts: 2308
    Originally posted by Campbell Ritchie:
    You might be overlooking an Error which makes it impossible for your application to run properly anyway.

    Very correct.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: catch(Throwable t) ??