Exceptions: Throwing ,Propagation and Handling In Java
In my previous article i explained in detail about exception hierarchy in java. In this article we will go through about some important concepts on exception handling in java.
What happens when an exception is thrown in java :-
Whenever an exception occurs in a piece of code , an exception object is created and is handed over to the execution engine of jvm(learn about jvm in detail in this article). This handling over an exception to the execution engine is termed as throwing an exception.

In the example code shown above, the main method creates and throws an IllegalArgumentException.In our case the main method is the first and only method in the call stack, when the execution didn't find any suitable catch block which can handle the exception it gives the exception to Default Exception Handler. This handler prints the exception in the following format and the program is terminated abnormally.
Exception in thread "xxx" ExceptionName :Description
//stack trace
In general ,there might be cases when lots of methods are called before the method which actually generates the exception. the method call sequence is maintained in the call stack in the stack memory area of jvm. The execution engine looks backward in the same sequence the methods are called to look for an appropriate block of code. If an appropriate catch block is found the exception is handled there otherwise it is given to the default exception handler.
I hope from the detailed example , now you know how to create an exception class and how to throw that class's object and what happens when you throw that object.In later articles i will cover how to write try ,catch and finally blocks for proper handling of codes.
What happens when an exception is thrown in java :-
Whenever an exception occurs in a piece of code , an exception object is created and is handed over to the execution engine of jvm(learn about jvm in detail in this article). This handling over an exception to the execution engine is termed as throwing an exception.

In the example code shown above, the main method creates and throws an IllegalArgumentException.In our case the main method is the first and only method in the call stack, when the execution didn't find any suitable catch block which can handle the exception it gives the exception to Default Exception Handler. This handler prints the exception in the following format and the program is terminated abnormally.
Exception in thread "xxx" ExceptionName :Description
//stack trace
In general ,there might be cases when lots of methods are called before the method which actually generates the exception. the method call sequence is maintained in the call stack in the stack memory area of jvm. The execution engine looks backward in the same sequence the methods are called to look for an appropriate block of code. If an appropriate catch block is found the exception is handled there otherwise it is given to the default exception handler.
I hope from the detailed example , now you know how to create an exception class and how to throw that class's object and what happens when you throw that object.In later articles i will cover how to write try ,catch and finally blocks for proper handling of codes.
Comments
Post a Comment