Our social:

Thursday, 24 March 2016

Exception Handling Part2

Now we are going to continue from last post:
Inside a method if any exception occured the method in which it is raised is responsible to create exception object by including the following information 
1. Name of exception
2. Description of exception
3. Location at which exception occurs(stack trace)

After creating exception object, method handovers that object to the jvm.
JVM will check wether the method contains an exception handling code or not. if the method doesnot contain exception handling code then, JVM terminates that method abnormally and removes crossponding entry from the stack.

Then JVM identifies caller methods and checks wether caller method contains any Exception handling code or not. If the caller method doesnot contain any handling code then JVM terminates tthat caller methods also abnormally and removes the crossponding entery from the stack

This process will be continued until main method, if the main() also doesnt contain handling code the JVM will terminates main() abnormally and removes crossponding entry from the stack also.

Then JVM handovers responsibility of exception handling to DefaultExceptionHandler, which is the part of JVM. DefaultExecptionHandler prints exception information in the following formate and terminates program abnormally.


EX:
Exception in thread "xxx" name of the Exception:Description 
Stack Trace

----------------------------------------------

code example(code snipp):
package com.smartyy.exceptionHandlingWorld;
class Test{
public static void main(String[] arg){
doStuff();
}
public static void doStuff(){
doMoreStuff();
}
public static void doMoreStuff(){
System.out.println(10/0);
}
}

Example2

class Test{
public static void main(String[] arg){
doStuff();
System.out.println(10/2);
}
public static void doStuff(){
doMoreStuff();
System.out.println("hi");
}
public static void doMoreStuff(){
System.out.println("Hello");
}
}

NOTE:
In a program if atleast one method terminates abnormally then the program termination is abnormal termination,
If all methods are terminated normally then only program termination is normal termination.

----------------------------------------

Exception Hirarchy:
Throwable acts as root for java exception hirarchy 
Throwable defines two child classes
1. Exception
2. Error

There is bit confusion between Exception and Error. so here we are going to discuss little things about both and we will continue in our next comming post that will make you understand easily.

Exception:
Most of the times Exceptions are caused by our programs and these are recoverable for example: if our progamming requirment to read data from remote file locating at londonat runtime if remote file is not available then we will get runtime exception called FileNot FoundException.
If FileNotFoundException occurs we can provides local file and continue rest of the progam normally.
   try{
read data from remote file locating at london
}catch(FileNotFoundException fnfe){
read data from local file
}

Error:
Most of the times errors are not caused by our program and these are due to lack of system resources. Eerrors are non-recoverable.for example if OutOfMemoryError occurs being a programmer we cant do anything and the program will be terminated abnormally.
Syetem admin or server admin is responsible to increase Heap memory.

0 comments:

Post a Comment