This article article illustrates the Java flow to handle an exception. Java uses 'try' and 'catch' blocks for the code that handles the exception, and a 'throw' keyword used in the method where the exception occurs. There are 4 sections to the reading, each after the first accessed by clicking <next>. Be sure to read each.
A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of code. When an exceptional condition arises, an object representing that exception is created and thrown in the method that caused the error. That method may choose to handle the exception itself or pass it on. Either way, at some point, the exception is caught and processed.
The programs you write can generate many types of potential exceptions, such as when you do the following:
In Java there are three types of loops:
These errors are called exceptions because, presumably, they are not usual occurrences; they are “exceptional.” The object-oriented techniques to manage such errors comprise the group of methods known as exception handling.
Exception handling works by transferring the execution of a program to an appropriate exception handler when an exception occurs. Let’s take an example program which will do take two numbers from user and print division result on screen. This might lead to exception condition if the denominator is zero.
Outputs based on user input combinations:
There are two ways of handling the exception, first catch the exception and take corrective action or throws the exception to the calling method which will force the calling method to handle it.
Outputs based on user input combination:
When a Java method is going to throw an exception, to indicate that as part of the method signature ‘throws‘ keyword should be used followed by the exception. It means that the caller of this method should handle the exception given in the throws clause. There can be multiple exceptions declared to be thrown by a method. If the caller of that method does not handle the exception, then it propagates to one level higher in the method call stack to the previous caller and similarly till it reaches base of the the method call stack which will be the java’s runtime system. For this approach, we use throws keyword in method declaration which will instruct the compiler to handle exception using try-catch block. When we add throws keyword in divide method declaration compile time error will be seen as below,
As you can see either we can surround the code with try-catch block or we can re-throw it to be handled by calling the method. In this case, we are calling a method from the main() method so if we re-throw the exception it would be handled by JVM. Let us update the code and see output based on input combination
Outputs based on user input combination:
Nested Try-catch block:
The try statement can be nested. That is, a try statement can be inside the block of another try.Each time a try statement is entered, the context of that exception is pushed on the stack. If an inner try statement does not have a catch handler for a particular exception, the stack is unwound and the next try statement’s catch handlers are inspected for a match. This continues until one of the catch statements succeeds, or until all of the nested try statements are exhausted.If no catch statement matches, then the Java runtime system will handle the exception. Below is the syntax of nested try-catch block.
Use of finally block:
When you have actions you must perform at the end of a try...catch sequence, you can use a finally block. The code within a finally block executes regardless of whether the preceding try block identifies an Exception. Usually, you use a finally block to perform cleanup tasks that must happen whether or not any Exceptions occurred, and whether or not any Exceptions that occurred were caught. In an application where database connection, files are being operated, we need to take of closing those resources in exceptional condition as well as normal condition.
We have learned about how exceptions are generated and various ways of handling exceptions. Catching exception or propagating exceptions. We have learned keywords like try, catch, finally, throws and programmatic use of these keywords.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License.