15 Exception Handling

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 the try-catch-finally construct. Exceptions thrown by the try block of code are 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 the default exception handler. The 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.

27 thoughts on “15 Exception Handling

  1. 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

  2. Awesome article! It is in detail and well formatted that i enjoyed reading. which inturn helped me to get new information from your blog. After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article.Data Science Training In Chennai

    Data Science Online Training In Chennai

    Data Science Training In Bangalore

    Data Science Training In Hyderabad

    Data Science Training In Coimbatore

    Data Science Training

    Data Science Online Training

Leave a Reply

%d bloggers like this: