對異常類的理解(程式輸出)
阿新 • • 發佈:2022-05-20
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