I have the below two sets of code
First Set of code is as below:
public static void main(String[] args){
try {
main(null);
} catch (Throwable e) {
}
System.out.println("Value of args[0] is : "args[0]);
}
Output is :
Value of args[0] is : db
Second set of code is as below:
public static void main(String[] args){
try {
main(null);
} catch (StackOverflowError e) {
}
System.out.println(args[0]);
}
Output is :
Exception in thread "main" java.lang.NullPointerException
at com.way2learnonline.ui.Demo.main(Demo.java:16)
In both cases I am passing a command line argument i.e. 'db'.
In first set of code I am catching Throwable in the catch block where I can access the command line argument i.e. args[0]
(I can see the args[0] output in the console).
In second set of code I am catching the StackOverflowError where I can not access the args[0]. It is showing NullPointerException.
I am not able to understand the behavior of Java.
Why I can access args[0] in first case and why args is null in second case.
Can someone explain why java is behaving like this?