Exception? What does that mean?
"Exception" in general means something which is not usual or something which is different from normal. For example, taking tablets or some medicine can be considered as an exception. Because they do not happen normally but in some unusual situations.
By the way "Exception" in programming means any event that interrupts the normal execution of the program. so now this goes in hand with the general definition of exception.
-------------------------------------------------------------------------------------------------------------------
| Exception is an event that interrupts the normal execution of the program |
-------------------------------------------------------------------------------------------------------------------
Why Should we handle exception?
Handling exception may seem to be a weird stuff for a beginner and there are all the possibilities that the beginners end up with this question.
For beginners the programs which they work on, for learning will be small and simple and they surely will not be in a need to handle exception. But, when the software size grows, its necessary to handle the exception, especially for the software that are designed for commercial purpose.
Now here is a simple example to explain the importance of Exception handling..
consider the software used in banking systems. If the software crashes due to some exception then there would be so much loss in terms of money.
So its always good that the software don't stop abruptly. But if the error is so fatal that the software has to stop then at least some measures to store the information so that it can be recovered after the crash. This obviously shows that exception handling is necessary.
Now let's get into Java
Exception handling in Java
To handle exception in Java is very simple, just place the suspicious code in try block and the handling code in catch block.
General Syntax
try
{
// code that can cause exception
}
catch( /*Exception class*/ )
{
// code to handle the exception
}
catch(){}
:
:
finally
{
//code that will be executed always
}
Execution of try catch block
The above block diagram shows the working of Try catch block.
Statements that are in try block will be executed and if any exception occurs then the statements in corresponding catch block will be executed. if no exception occurs then catch block will not be executed.
Code sample
try
{
int a=10,b=0,c;
c=a/b; //Division by zero error occurs in this statement
System.out.println(c);
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurred") ; //displays the error message
}
catch(Exception e)
{
System.out.println(e.toString());
}
finally
{
System.out.println("Finally block"); // This statement will always be executed
}
In the above example when the error occurs it is caught by the catch block and the error message is displayed and then finally block will be executed.
If the statement doesn't have any error for example if value of b is not zero then there will not be any exception so catch block will not be executed, but finally block will be executed.
In the catch block "ArithmeticException" is the class name of the Exception to be caught and "e" is the object which can be used to get information about the exception that was caught by that block.
If any other Exception is to be caught then corresponding Exception class names can be used. To catch all kinds of Exception "Exception" class can be used. "Exception" is the base class for all Exception classes. Therefore "ArithmeticException" is a derived class of "Exception".
In the above example both "Exception" and "ArithmeticException" are used and if any arithmetic exception occurs then only the catch block with "ArithmeticException" will be executed. But if any other exception occurs then the catch block with "Exception" will be executed. This is because if exception occurs then the catch blocks will be checked for any match and if anything is found then it will be used and if not found then catch blocks will be searched for any base class and it will executed.
Exceptions need be predefined but they can also be user-defined.
This is the basic Exception handling in Java.