Exception handling in javaException handling in java is the most important topic. Almost each and every application can have errors at run time which can cause the application to stops it’s execution. Think of it, if each and every time an error is coming and you just have to start the application again and again then how would you will feel.
So in this tutorial, we will discuss how to prevent our application from errors in java, implementation and some good examples to demonstrate it’s flow.
But before we proceed, it’s important to know about the term exception. So, let’s first understand it with a real world example.
let’s take a real world example to understand exception and error.
It’s shiny day today and your plan is to go on long drive in your car. You packed your bags, stuff and put them in the back seat. You started your journey with some hard rock music and you are singing along.
You are now on highway and you are just thinking of some fast and furious today. So you are now at the speed of around 100 or 120 km/h or more. While you are on your top speed then suddenly an animal appears in the middle of the road.
Two things are gonna happen to you now, let’s see them.
Error : You will not be able to control your car, controlled car will hit something and next you know what’s gonna happen to you – This is called error at run time in your application. Just dead, nothing else gonna happen.
Exception : Somehow, you will save the animal and your self. You will be able to control your car and that is worth to watch. – This is called Exception at run time. You are able to control the flow of your application.
So from above example, the definition is clear.
Exception is an event which occurs while application is running and it may disturb or terminate the application flow. On the other hand, Exception handling is the process of overcoming from that situation to maintain the application flow without any disturbance.
Types of Exceptions in java
There are different types of exceptions which may occur while your application is running or compiling. So let’s discuss them one by one.
- Checked Exceptions
Checked exceptions are those exceptions which are checked during the compilation of your application. Your program will not compile until you fix those error. Like IOException, SQLException etc.
- Unchecked Exceptions
Unchecked exceptions are those exceptions which occurs while your application is running. Anything can happen which will disturb the normal flow of the application flow. But you can handle those exceptions like NullPointerException, ArithmeticException etc.
Error is something which can not be handled. Application will simply stop it’s execution and you can not do anything about it. Some errors are like OutOfMemoryError, VirtualMachineError etc.
Exception Class and Interface Hierarchy
Java provides us some classes and interface which you can use to handle run time, compile time or errors in your application. So let’s first look at the hierarchy of exception.
The java.lang.Throwable class is the root class of all the exception or error class in java. Only instance of this class will be thrown by the java virtual machine.
What happens when an exception occurs at run time, how JVM handles it
Now, let’s discuss some internal details of exception handling process by java virtual machine. What happens when an error or exception occurs at run time and what steps java virtual machine takes to recover it.
- When an error occurs in a particular method then that method creates an object. Object called “Exception” object which holds the information about the error.
- This exception object is then passed to run time system for further processing. When object is created and transferred to the run time system, it is called throwing (we will discuss about throw keyword later).
- After that, run time system tries to find the appropriate handler to handle the error which occurred. Run time system checks the method from the call stack (A separate method call stack is created when a particular exception occurs which provides details information from where the exception is thrown) which contains code block to handle the exception. This block of code is called exception handler.
- When run time system finds the appropriate handler, it passes the exception object to the appropriate handler. And this process is called “catch” the exception.