I know that we should not handle Error in java using try catch, but I tried it for learning and found that we can handle Error similarly Exception using try catch block. Even code after catch block is also getting execute. I generated a StackOverFlowError but still I can initialize local variable. Can any one please explain it?
import java.util.LinkedList;
import java.util.List;
public class ExceptionDemo {
public static void main(String[] args) {
try{
method1();
}
catch(Error e){
System.out.println(e.getStackTrace());
System.out.println("Hello world");
int a =10;
System.out.println(a);
method();
}
System.out.println("Hello world2");
}
public static void method1(){
method1();
}
public static void method(){
List l1 = new LinkedList();
l1.add("A");
int[] aa = new int[10000];
aa[0]=25;
System.out.println(aa[0]);
int b =10;
int c = 20;
System.out.println( b +""+c);
}
}
Output of this class is -
[Ljava.lang.StackTraceElement;@1db9742
Hello world
10
25
1020
Hello world2