| |
An EXCEPTION, is an abnormal event in a program, that means an Exception breaks the normal flow of execution. There are several reasons which leads to exceptions including hardware failures, resource exhaustion. When an exceptional event occurs in a Java program the normal flow will be break down and then the control has to go into some other place, this is something called as an Exception that is "thrown." The code that's responsible for doing something about the exception is called an "Exception Handler," and it "catches" the thrown exception.
Exception handling works by transferring the execution of a program to an appropriate exception handler when an exception occurs. For example, if you call a method that opens a file but the file cannot be opened, execution of that method will stop, and code that you wrote to deal with this situation will be run. Therefore, we need a way to tell the JVM what code to execute when a certain exception happens.
Using try and catch
We use the try and catch keywords. The try is used to define a block of code in which exceptions may occur. This block of code is called a guarded region (which means "risky code is there in this block"). One or more catch clauses match a specific exception to a block of code that handles it.
Suppose lets check the following code.
Public class ExceptionTest
{
try
{
-----------------------
-----------------------
-----------------------
}
catch(MyExceptionOne)
{
---------------------
---------------------
}
catch(MyExceptionTwo)
{
-------------------
-------------------
}
}
In the above class the block which is enclosed by try keyword is guarded region. That is we are explicitly saying to the JVM that here some abnormal event may occur and the block which are enclosed with the catch keyword is called Exception handler block. Whenever an abnormal event occurred then the control will go into this block. Notice that the catch blocks immediately follow the try block. This is a requirement; if you have one or more catch blocks, they must immediately follow the try block. Additionally, the catch blocks must all follow each other, without any other statements or blocks in between and also, the order in which the catch blocks appear matters.
Suppose lets say the execution is started, the control is entered into try block an exception has occurred at line one of try block then the control will immediately pass into one of the its catch blocks and executes. Here the remaining lines of the try block will not execute. After executing a catch block the control will never come again into the try block instead it just executes statements after the catch blocks. Here when an exception occurred it checks its type and if a match is found at one of the catch blocks, then that catch block will be executed. For example an exception has occurred in try block which is of type MyExceptionOne then the control checks the matching catch block here the matched catch block is first catch block itself. So it'll be executed. If the try block is executed without any exceptions then the catch blocks will not be executed. The statements after the catch blocks will be executed. Example
Program that generates Arithmetic Exception
class TestException
{
public static void main(String args[])
{
int i=15;
double d;
try
{
for(int j=0;j<=5;j++)
d=i/j;
}
catch(Exception e){
System.out.println("The generated Exception is: "+e);
}}}
We know that when 15 is divided by non zero value it'll give some result but when it is divided by zero the result we may not predict. So we have kept that code in try block and handled in catch block. If you execute above code the following out it gives.
Output:
The generated Exception is: java.lang.ArithmeticException :/(divided by) byzero
// Venugopal Darur
|