try-catch-finally
try-catch-finally. It's OK without the catch clause.try clause and one finally clause can be described. You can write multiple catch clauses.return statement in both the catch clause and the finally clause ↓
public class Test {
public static void main(String... args) {
System.out.println(getException());
}
private static String getException() {
try {
throw new Exception();
} catch (Exception e) {
return "catch!";
} finally {
return "finally!";
}
}
}
The execution result is
finally!
Throwable
-> Error
-> Exception
-> RuntimeException
Error
StackOverFlowtry-catch and throws are not enforced, can be writtenandRuntimeException`Exception
RuntimeException and its subclassestry-catch or throwsRuntimeException
try-catch and throws are not enforced, can be writtenRecommended Posts