//finally節はtry節のreturn/throwを上書きする。
//default文はcase文のreturn/throwを上書きしない(比較)。
class TestFinally{
void method() throws Throwable{
try{
throw new Exception();
}finally{
throw new Throwable();
}
}
}
class TestDefault{
void method() throws Throwable{
switch(123){
case 123:
throw new Exception();
default:
throw new Throwable();
}
}
}
public class Test {
public static void main(String[] args) {
try{
(new TestFinally()).method(); //①
(new TestDefault()).method(); //②
}catch(Exception e){
//②はこちらでcatchされる。
System.out.println(e);
}catch(Throwable e){
//①はこちらでcatchされる。
System.out.println(e);
}
}
}
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme