- Inside a method if any exception occurs, the method in which it is raised is responsible to create an exception object by including the following the information.
Name of exception
Description of exception
location at which exception occurs (stack trace)
- After creating exception method handover that object to the JVM.
- JVM will check whether the method contains any exception handling code or not. If the method doesn't contain exception handling code, then JVM terminates that method abnormally and removes the corresponding entry from the stack.
- Then JVM identify caller method and checks whether caller method any handling code are not.
- If the caller method doesn't contain handling code, then JVM terminates that caller method also abnormally and removes the corresponding entry from the stack
- this process continue until main method and if the main method also doesn't contain handling code then JVM terminate main method also abnormally and remove corresponding entry from the stack.
- Then JVM handovers responsibility of exception handling to default exception handler, which is the part of JVM default exception handler prints exception information in the following format and terminates program abnormally
eg-:
class DefaultException{
public static void main(String[] args){
dostuff();
}
public static void dostuff(){
doMoreStuff();
}
public static void doMoreStuff(){
System.out.print(10/0);
}
}
class DefaultException{
public static void main(String[] args){
dostuff();
}
public static void dostuff(){
doMoreStuff();
System.out.print(10/0);
}
public static void doMoreStuff(){
System.out.print("Hello");
}
}
output-: Hello
Exception at thread main
at dostuff()
and main()
class DefaultException{
public static void main(String[] args){
dostuff();
System.out.print(10/0);
}
public static void dostuff(){
doMoreStuff();
System.out.print("Hi");
}
public static void doMoreStuff(){
System.out.print("Hello");
}
}
output-: Hello
Hi
Exception at thread main
at main()
Note-: In a program if at least one method terminates abnormally then the program termination is abnormal termination.
If All method terminates normally, then only program termination is normal termination.
Exception hierarchy
Throwable-: Throwable class access root for Java exception hierarchy.throwable class defines two child classes
- Exception-: most of the time exceptions are caused by our program and these are recoverable for i.e if our programming requirement is to read data from remote file locating at Landon at run time remote file not available then we will get run time exception saying file not found exception. If a file not found exception occurs, we can provide a local file and continue the rest of the program normally.
Example-:
try{
read data from remote file location at Landon
}catch(FileNotFoundExceiption e){
use locale file and continue rest of program normally
}
2. Error-: most of the time Errors are not caused by our program and these are due to lack of system resources. Errors are non-recoverable. For example -; outofmemory error occurs being a programmer we can't do any thing and the program will be terminated abnormally. System admin or server admin is responsible to increase the heap memory.