Exception
An unexcepted unwanted event that disturb normal flow of Java program execution is known as an exception.
For Example-
- File not found exception in Java
- IOException
It is highly recomended to handel those exception so that normal flow of program execution continues as it is .
Exception handling doesn’t mean reparing an exception, we have to provide alternative way to continue program execution is a concept of exception handling.
Example-
In a Java program, we have to read data from a file that is located at another location. If the file is unavailable for some reason, then our Java program should not be terminated abnormally. We have to provide some alternative logic for this condition so that our program will keep running if the file is not available. This alternative logic is nothing but exception handling.
Default Exception Handling
If an exception occurs inside a Java method, the same method is responsible for the exception object. The exception object consists of the following information-
- Name of the Exception
- Description of Exception
- Location at which Exception occurs(stack trace)
After making the exception object method gives that object to the JVM.
Not JVM will check whether the method contains any exception handling code or not. If there is no code for exception handling in the method then JVM will terminate that method abnormally and remove the current entry(method) from the stack.
After that, JVM identifies the caller method and checks whether the caller method contains any exception handling code or not. If the Caller method doesn’t contain an exception handling code then JVM will terminate that caller method abnormally, and remove the caller method entry from the stack. The same procedure will continue until the main
method and if there is no exception handling code in the main method then JVM will also terminate the main method abnormally and remove the corresponding entry(main) from the stack, after that JVM handover the responsibility of exception handling to the default exception handler which is part of JVM. The default exception handler prints exception information in the following format and terminates program execution abnormally.
Exception in thread “ABC” name of Exception: Description stack track.
For Example in the below code if we try to divide a number by zero then we will get following exception – “
Exception in thread “main” java. lang.ArithmeticException: / by zero
at Exception.ExceptionExample.Division(ExceptionExample.java:5)
at Exception.ExceptionExample.main(ExceptionExample.java:9)”.
package Exception;
public class ExceptionExample {
public static void Division (int a, int b){
System.out.println( a+ " /" + b + "= " + a/b);
}
public static void main(String[] args) {
Division(50, 0);
}
}
Note-. In a program at least one method terminates abnormally then the program termination is abnormal, A Java program termination is normal termination if and only if all methods terminated normally.
Similar Java Tutorials
- Odd Even in Java
- Fibonacci Series Java
- Switch statements in Java
- For loop in Java
- While loop in Java
- Do while loop in Java
- Import statement in Java
- Abstract modifier in Java
- Strictfp modifier in Java
- Final variable in Java
- Adapter Class in Java
- Method signature in Java
- Method overloading in Java
- Has A Relationship
- Inheritance in Java
- Encapsulation in Java
- Abstraction in Java
- Data Hiding in Java
- Interface vs abstract class
- Method Overriding in Java
- Method Overloading in Java
- Coupling in Java
1 thought on “Exception Handling in Java”