15 Exception Handling

Exceptions in Java are objects. All exceptions are derived from the java.lang.Throwable class. Exceptions can be handled in Java using try-catch-finally construct. Exceptions thrown by the try block of code is caught (handled) by the catch block.

The following code shows an example of the try-catch-finally construct.





The output of the above example




Whenever an exception is thrown by try block of code, it looks for catch construct which handles that exception. If no catch construct is found which handles the exception, then the exception is handled by default exception handler. Catch construct is not executed if no exception is thrown or if the respective catch construct does not handle the specific exception. For example, in the code above, the catch construct only catches ArithmeticException. Any other exception, say FileNotFound will not be handled by this catch construct. Finally, the code block is executed, no matter an exception is thrown or not. An example for use of finally construct can be to write a code that closes DB connections. This will ensure that DB connection is closed, no matter an error happens or not during data retrieval from DB.



8 comments:

Rohini .R said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
Selenium Training in Bangalore | Selenium Training in Bangalore | Selenium Training in Bangalore | Selenium Training in Bangalore

cathryn leno said...

I am really happy with your blog because your article is very unique and powerful for new reader.
Click here:
selenium training in chennai
selenium training in bangalore
selenium training in Pune
selenium training in pune
Selenium Online Training

Richa T said...

Thank you so much for a well written, easy to understand article on this. It can get really confusing when trying to explain it – but you did a great job. Thank you!

Java training in Chennai | Java training in USA |

Java training in Bangalore | Java training in Indira nagar | Java training in Bangalore | Java training in Rajaji nagar

nivatha said...

This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
Data Science training in rajaji nagar | Data Science Training in Bangalore
Data Science with Python training in chennai
Data Science training in electronic city
Data Science training in USA
Data science training in pune

sai said...

Thank you for an additional great post. Exactly where else could anybody get that kind of facts in this kind of a ideal way of writing? I have a presentation next week, and I’m around the appear for this kind of data.

python training Course in chennai
python training in Bangalore
Python training institute in bangalore

saran latha said...

I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 
Devops Training courses
Devops Training in Bangalore
Best Devops Training in pune
Devops interview questions and answers

Anonymous said...

It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...

angularjs Training in chennai

angularjs-Training in tambaram

angularjs-Training in sholinganallur

angularjs-Training in velachery

angularjs Training in bangalore

Dharani M said...

selenium training centers in Marathahalli

best software testing training institutes in Marathahalli with placements

automation testing courses in Marathahalli

selenium testing course in Marathahalli

software testing institutes in Marathahalli

selenium training in Marathahalli

best selenium training in Marathahalli

selenium course in Marathahalli