Exception: An exception is an unwanted or unexpected event, that disturbs the normal flow of the program i.e at run time is called an exception.
It is highly recommended to handle exceptions so that the program terminates normally. The main objective of exception handling is graceful (normal) termination of the program.
What is the meaning of exception handling?
Exception handling doesn’t mean we are repairing an exception. We have to define an alternative way to continue the rest of the program normally without any disturbance. This way of defining alternative is nothing but exception handling.
Example: Suppose our programming requirement is to read data from a remote file locating in India. At runtime, if the India file is not available then our program should not be terminated abnormally.
We have to provide a default local file to continue the rest of the program normally when the file is not found at runtime. This way of defining alternative is nothing but exception handling.
statement 5;//exception occurs
read data from London file
use a local file and continue the rest of the program normally
Suppose there are 10 statements in your program and there occurs an exception at statement 5 like FileNotFoundException, then the rest of the code will not be executed and the program terminated abnormally. If we provide a default local file to perform exception handling at statement 5, then the rest of the statement will be executed. That is why we use exception handling in Java.
Default exception handling in java:
- If an exception raised inside any method then that method is responsible to create an Exception object with the following information. Name of the exception.
- After creating that Exception object, the method handovers that object to the JVM.
- JVM checks whether the method contains any exception handling code or not. If the method won’t contain any handling code then JVM terminates that method abnormally and removes the corresponding entry from the stack.
- JVM identifies the caller method and checks whether the caller method contains any handling code or not. If the caller method also does not contain handling code then JVM terminates that caller method also abnormally and removes the corresponding entry from the stack.
- This process will be continued until main() method and if the main() method also doesn’t contain any exception handling code then JVM terminates main() method also and removes the corresponding entry from the stack.
- Then JVM handovers the responsibility of exception handling to the default exception handler.
- Default exception handler just prints exception information to the console in the following format and terminates the program abnormally.
Hierarchy of Java Exception classes:
The Throwable class is the root class of Java Exception hierarchy, which is inherited by two subclasses: Exception and Error. Throwable class present in java.lang package.
Different Between Exception and Error:
Most of the case exceptions are caused by our program and these are recoverable by the programmer.
Ex: If FileNotFoundException occurs then we can use local file and we can continue rest of the program execution normally.
Most of the cases errors are not caused by our program these are due to lack of system resources and these are non-recoverable by programmers.
Ex: If OutOfMemoryError occurs being a programmer we can’t do anything the program will be terminated abnormally. System Admin or Server Admin is responsible to raise/increase heap memory.
Types of Java Exceptions:
There are mainly two types of exceptions: checked and unchecked.
- Checked Exception
- Unchecked Exception
So if we are talking about an error then an error is considered as the unchecked exception.
Checked Vs Unchecked Exceptions:
The exceptions which are checked by the compiler whether programmer handling or not, for smooth execution of the program are called checked exceptions. Checked exceptions are checked at compile-time but it is always occurring at runtime.
IOException, SQLException, etc.
The exceptions which are not checked by the compiler whether programmer handing or not, are called unchecked exceptions. Unchecked exceptions are not checked at compile-time, but they are checked at runtime.
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.
Fully checked Vs Partially checked :
A checked exception is said to be fully checked if and only if all its child classes are also checked.
A checked exception is said to be partially checked if and only if some of its child classes are unchecked.
- RuntimeException and its child classes, Error, and its child classes are unchecked and all the remaining are considered as checked exceptions.
- Whether exception is checked or unchecked compulsory it should occur at runtime only and there is no chance of occurring any exception at compile time.
- The only possible partially checked exceptions in java are Throwable and Exception.
Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.