For the last few days I am trying to dig deep in java, I came across try catch finally
exception handling.
I know finally executes no matters what, but I want to know "WHY" we can also handle the exception in catch or after catch, why finally?
I came up with an answer
https://stackoverflow.com/a/23537289/7794329
public class tryCatch {
public static void main(String[] args) {
double p = 1.0D;
String str = "bla";
try{
p = Double.valueOf(str);
}
catch(Exception ex){
System.out.println("Exception Happened");
return; //return statement here!!!
}finally{
System.out.println("Finally");
}
System.out.println("After finally");
System.out.println("again finally");
}
}
output
Exception Happened
Finally
what if i remove that return; and handle after the code of finally. It will run.. so why we need finally only because of return statement? is it like i am doing mistakes by returing the code and forcefully handling it in finally.I can also make a flag in catch and put it in a if else block to handle the exception in catch
I really completely didn't understand can someone let me understand in plain language? Why do we use finally if we can handle it from try catch or even catch fails we can handle it in if else
like if catch got exception run this... else...