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.



14 comments:

Unknown 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

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

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



jefrin said...

Good to read thanks for sharing
DevOps Training Institute in Chennai

Business said...

Thank you so much for posting this. I really appreciate your work. Keep it up. Great work!Best software training company with placement in Hyderabad

Jagadeesh said...
This comment has been removed by the author.
prash p said...

Awesome information.
such an useful article.
thanks for posting.keep sharing.

priya rajesh said...

Excellent blog with unique content, thanks a lot for sharing this. I love to learn more about this topic.
Data Science Training in Chennai
Data Science Certification in Chennai
R Programming Training in Chennai
R analytics Training in Chennai
Machine Learning Training in Chennai
Machine Learning course
Data Science Training in Velachery
Data Science Course in Chennai

jeya sofia said...

Excellent idea!!! I really enjoyed reading your post. Thank you for your efforts . Share more like this.
Angularjs Training in T Nagar
RPA Training in T nagar
Software testing training in Tambaram
Dot Net Training in Velachery
Web Designing Course in T Nagar
Spoken English Classes in Velachery
German Classes in chennai
SEO Training in Anna Nagar
selenium training in tambaram
java training in tambaram

EXCELR said...

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

360digitmg said...

It is imperative that we read blog post very carefully. I am already done it and find that this post is really amazing.
business analytics course

Unknown said...

Amazing blog post , thanks for sharing with us

Best Digital Marketing Training in Bangalore

Learn Digital Academy said...

Thank you for sharing such a piece of beautiful information. I hope you will keep sharing such wonderful information in the future.
It is amazing and wonderful to visit your site. We are providing
best Digital marketing courses in Bangalore
and graphic design courses in bangalore

Keerthi55 said...

I really enjoy the blog article.Much thanks again.
MuleSoft online course
MuleSoft onlinetraining from india