Whenever inside a method, if an exception has occurred, the method creates an object known as exception object and hands it off to the run-time system(JVM). An exception object is an instance of an exception class. It gets created and handed to the Java runtime when an exceptional event occurred that disrupted the normal flow of the application. This is called “to throw an exception” because in Java you use the keyword “throw” to hand the exception to the runtime. When a method throws an exception object, the runtime searches the call stack for a piece of code that handles it. The exception object contains name and description of the exception, and current state of the program where exception has occurred. Creating the Exception Object and handling it to the run-time system is called throwing an exception. There might be the list of the methods that had been called to get to the method where exception was occurred. This ordered list of the methods is called Call Stack. Now the following procedure will happen.
See the below diagram to understand the flow of the call stack.
Output
Let us see an example that illustrate how run-time system searches appropriate exception handling code on the call stack.
Output