LoginSignup
0
0

More than 5 years have passed since last update.

finally節とreturn/throw

Last updated at Posted at 2016-07-25



//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);
        }
    }
}



0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0