对异常类的理解(程序输出)

public class Test{
    public static void main(String[] args) {
        String test = "yes";//no
        try{
            System.out.println("start try");
            doRisky(test);//若在此处抛出异常,后面的一句不执行
            System.out.println("end try");
        }
        catch(ScaryException se){
            System.out.println("scary exception");
        }
        finally{
            System.out.println("finally");
        }
        System.out.println("end of main");
    }
    static void doRisky(String test) throws ScaryException{
        System.out.println("start risky");
        if("yes".equals(test)){
            throw new ScaryException();//若在此处抛出异常,后面的一句不执行
        }
        System.out.println("end risky,在test是no的时候会输出,在test是yes的时候不会输出,");
        
    }
}
class ScaryException extends Exception{

}
//Answer:

//start try
//start risky
// end risky
// end risky
//finally
//end of main

//start try
//start risky
//scary exception
//finally
//end of main

 

posted @ 2022-05-19 21:58  HzzzzLi  阅读(32)  评论(0)    收藏  举报