What's important to understand here is that when you throw an exception, the rest of the code is skipped. In this case this means that System.out.println("hai");
is always skipped, since you throw an exception in both branches of the if-statement. If it's always skipped, it's unreachable!
Here's an illustration:
try {
int a=10, b=0;
if(a<b) {
throw new Exception("false");
}
String n = "30.0";
float ff = (float) 0.0;
if (Float.parseFloat(n) < ff) {
throw new Exception("big"); ----------------------.
} else { |
throw new Exception("add"); ------------------. |
} | |
System.out.println("hai"); //unreachable code | |
} catch(Exception e){ | |
/* execution continues here */ <----------------+---'
e.printStackTrace(System.err);
}
If you want the "hai"
to be reachable, you'll have to move it to a place where it's not always skipped. For instance to below the catch block:
try {
int a=10, b=0;
if(a<b) {
throw new Exception("false");
}
String n = "30.0";
float ff = (float) 0.0;
if (Float.parseFloat(n) < ff) {
throw new Exception("big");
} else {
throw new Exception("add");
}
} catch(Exception e){
e.printStackTrace(System.err);
}
System.out.println("hai"); // reachable!