[HD Pg 374, Sec. 10.5.0 - exercises]
Posted: Sat Jun 08, 2024 3:03 pm
weird, end of the exception chapter and have nothing seen about return in try/catch/finally
gotta learn it by myself trough tests and here i'm sharing:
RETURN "2" from try cuz no exceptions
RETURN "finally" because it overrides the try return
RETURN "finally" because it overrides the catch return
HERE it's very special, RETURN "finally" without even showing catch exception and program doesn't interrupt
Another interesting thing is that if i have a throw for all then compiler won't complain for a missing return statement. It will only complain if catch/finally omit them
gotta learn it by myself trough tests and here i'm sharing:
RETURN "2" from try cuz no exceptions
Code: Select all
static String m(){
try {
return String.valueOf(5/2);
} catch (Exception e){
return "catch";
}
}
Code: Select all
static String m(){
try {
return String.valueOf(5/2);
} catch (Exception e){
return "catch";
}
finally {
return "finally";
}
}
Code: Select all
static String m(){
try {
return String.valueOf(5/0);
} catch (Exception e){
return "catch";
}
finally {
return "finally";
}
}
Code: Select all
static String m(){
try {
return String.valueOf(5/0);
} catch (Exception e){
throw new Exception();
}
finally {
return "finally";
}
}
Code: Select all
static String m(){
try {
throw new RuntimeException();
} catch (Exception e){
throw new RuntimeException();
} finally {
throw new RuntimeException();
}
}