Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Feb 16, 1999

Handling Checked Exceptions

There are three ways to deal with checked exceptions in Java. First, you can declare that the "callee" method throws them (using the "throws" keyword). You should do this when your program cannot handle the exception at this point in the code. The method that declares the exception in its signature allows the exception to propagate one level higher in the stack. The calling method will have to handle the exception or pass it up to its caller.

Second, you can catch the checked exceptions in a try-catch block and handle them. You should do this if your program is capable of handling the exception at that point in the code. Just catching an exception and printing out a stack trace by calling printStackTrace() is great for debugging, but should not go out in production code.

Finally, you can catch checked exceptions in a try-catch block and re-throw them. You should this in two situations. The first is if you can partially handle the condition by cleaning up some of the resources. However, you still need the calling method to handle the exception itself. The other situation is if you want to convert the exception. You may want to do this in order to encapsulate a third-party exception into your own user-defined exception.

Ajit Sagar
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date