Java Exception:-
An unexpected unwanted event that disturbs normal flow of the program, is called an exception.
The purpose of exception handling:-
The main objective of exception handling is "graceful termination " of the program. It is highly recommended to handle the exception in java program.
Explanation:-
Exception Handling doesn't mean repairing an exception. We have to provide an alternative way so that the rest of the program will execute normally, is the concept of exception handling.
In our java program, there is a requirement of reading data from a remote file which is locating at NewYork (USA). At runtime, if NewYork file is not available our program should not be terminated abnormally. We have to provide some local file to continue rest of the program normally.
This whole process is known as "Exception Handling".
try
{
read data from remote file locating at NewYork
}
catch(FileNotFoundException e)
{
use local file & continue rest of the program normally
}
Runtime Stack Mechanism
- For every thread, JVM will create a Runtime Stack.
- Each and every method call performed by that thread will be stored in the corresponding stack.
- Each entry in the stack is called stack frame or activation record.
- After completing every method call, the corresponding entry from the stack will be removed.
- After completing all method calls the stack will become empty and that empty stack will be destroyed by JVM just before the terminating the thread.
class Test
{
public static void main(String [] args)
{
doStuff();
}
public static void doStuff()
{
doMoreStuff();
}
public static void doMoreStuff()
{
System.out.println("Hello World");
}
}
Default Exception Handling in Java
- Inside a method, if any exception occurs the method in which it has risen is responsible for creating exception object including the following information.
a. Name of the exception
b. Description of exception
c. Location of which exception occurs{Stack Track}
- After creating exception object method handovers that object to the JVM.
- JVM will check whether the method contain any exception handling code or not? If the method doesn't contain exception handling code then JVM terminated that method abnormally and remove the corresponding entry from the stack.
- Then JVM identifies the caller method and checks whether caller method contains any handling code or nor.
- If the caller method doesn't contain handling code then JVM terminates that caller method also abnormally and corresponding entry from the stack.
- This process will continue until main( ) method and if the main() method also does not contain handling code then JVM terminates main() method also abnormally then the corresponding entry from the stack.
- Then JVM handover responsibility of exception handling to default exception handler which is also part of JVM.
- Default exception handler prints exception information in the following format and terminate program abnormally.
Some examples of exception:-
class Test
{
public static void main(String [] args)
{
doStuff();
}
public static void doStuff()
{
doMoreStuff();
}
public static void doMoreStuff()
{
System.out.println(10/0);
}
}
Exception in thread main java.lang.ArithmaticException / by zero
at Test.doMoreStuff()
at Test.doStuff()
at Test.main()
class Test
{
public static void main(String [] args)
{
doStuff();
System.out.println(10/0);
}
public static void doStuff()
{
doMoreStuff();
System.out.println("Hiii");
}
public static void doMoreStuff()
{
System.out.println("Hello");
}
}
Hello
Hiii
Exception in thread main java.lang.ArithmaticException / by zero
at Test.doMoreStuff()
at Test.doStuff()
at Test.main()
Note: If in a java program at least one method terminates abnormally then the program termination is called abnormal termination.
If All methods terminated normally tthan only program termination is called normal termination.
No comments:
Post a Comment
Be the first to comment!
Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.
Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.
Regards,
JavaByChetan
Back To Home