Exception Handling Mechanism in Java
Exception:
An exception is a condition that is caused by a run-time error in the program. When the Java interpreter encounters an error such as dividing an integer by zero, it creates an exception object and throws it. If the exception object isn’t caught and handled properly, the interpreter will display an error message as given below:
Example:
class error { public static void main(String args[]) { int x=20; int y=10; int z=10; int a=x/(y-z); System.out.println("A ="+a); int b=x/(y+z); System.out.println("B ="+b); } }
Output:
java.lang.ArithmeticException: / by zero at error.main(error.java:20)
In Java, there are two types of exceptions exist:
- Checked Exceptions
- Unchecked Exceptions
Checked Exceptions:
These exceptions are explicitly handled in the code itself with the help of try-catch blocks. These exceptions are extended from the java.lang.Exception class.
Unchecked Exceptions:
These exceptions aren’t essentially handled in the program code. These exceptions are extended from the java.lang.RuntimeException class.
Exception Type | Description |
---|---|
ArithmeticException | It caused by math errors such as division by zero. |
ArrayIndexOutOfBoundsException | It caused by bad array indexes. |
ArrayStoreException | It caused by when a program tries to store the wrong type of data in an array. |
FileNotFoundException | It caused by an attempt to access a nonexistent file. |
IOException | It caused by general I/O failure, such as inability to read from a file |
NullPointerException | It caused by referencing a null object. |
NumberFormatException | It caused by when a conversion between strings and number fails. |
OutOfMemoryException | It caused when there's not enough memory to allocate a new object. |
SecurityException | It caused when an applet tries to perform an action not allowed by the browser's security setting. |
StackOverFlowException | It caused when the system runs out of stack space. |
StringIndexOutOfBoundsException | It caused when a program attempts to access a nonexistent character position in a string. |
Exception Handling Mechanism:
If the exception object isn’t caught and handled properly, the interpreter will display an error message and it will terminate the program. If we want the program to continue with the execution of the remaining code, then we should try to catch the exception object thrown by the error condition and then display an appropriate message for taking corrective actions. This task is called Exception Handling.
Purpose of Exception Handling :
1. Find or hit the exception.
2. Throw the exception.
3. Catch the exception.
4. Handle the exception.
Exception Handling consists of two segments:
- Detect the errors and throw exceptions.
- Catch exceptions and handle the exceptions
Syntax of Exception Handling :
....... ....... try { statement; // generates an exception } catch(Exception-type e) { statement; // processes the exception } ........ ........
Program:
class Error { public static void main(String args[]) { int a=10, b=5, c=5; int x, y; try { x=a/(b-c); // Exception here } catch(ArithmeticException e) { System.out.println("Division by Zero"); } y=a/(b+c); System.out.println("The value of y is :"+y); } }
Output:
Division by Zero
The value of y is : 1