標籤:方法 amp format 運行時異常 images instance 執行順序 while 策略
1.Aboutexception程式
代碼:
import javax.swing.*;class AboutException { public static void main(String[] a) { int i=1, j=0, k; //infinity正無窮 // k=i/j; try //監控{k = i/j; // Causes division-by-zero exception//throw new Exception("Hello.Exception!");}catch ( ArithmeticException e){System.out.println("被0除. "+ e.getMessage());}catch (Exception e){if (e instanceof ArithmeticException)System.out.println("被0除");else{ System.out.println(e.getMessage());}}finally { JOptionPane.showConfirmDialog(null,"OK"); } }}
運行結果:
修改後(刪除出現錯誤的語句)
Finally必定運行。
2.若將1中int類型改為double,輸出結果為infinity而非異常,因為javac在編譯時間將int語句產生為idiv位元組碼指令,而double則產生ddiv位元組碼指令,JVM在具體實現這兩個指令時,採用了不同的處理策略,導致兩段代碼運行時得到不同的結果。
3.閱讀代碼catchwho.Java,寫出運行結果
public class CatchWho { public static void main(String[] args) { try { try { throw new ArrayIndexOutOfBoundsException(); } catch(ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/內層try-catch"); } throw new ArithmeticException(); } catch(ArithmeticException e) { System.out.println("發生ArithmeticException"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/外層try-catch"); } } }
運行結果:
4.運行catchwho2並寫出結果:
public class CatchWho2 { public static void main(String[] args) { try { try { throw new ArrayIndexOutOfBoundsException(); } catch(ArithmeticException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/內層try-catch"); } throw new ArithmeticException(); } catch(ArithmeticException e) { System.out.println("發生ArithmeticException"); } //不允許累積錯誤 catch(ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/外層try-catch"); } } }
運行結果:
5.EmbedFinally.java
代碼:
public class EmbededFinally { public static void main(String args[]) { int result; try { System.out.println("in Level 1"); try { System.out.println("in Level 2"); // result=100/0; //Level 2 try { System.out.println("in Level 3"); result=100/0; //Level 3 } catch (Exception e) { System.out.println("Level 3:" + e.getClass().toString()); } finally { System.out.println("In Level 3 finally"); } // result=100/0; //Level 2 } catch (Exception e) { System.out.println("Level 2:" + e.getClass().toString()); } finally { System.out.println("In Level 2 finally"); } // result = 100 / 0; //level 1 } catch (Exception e) { System.out.println("Level 1:" + e.getClass().toString()); } finally { System.out.println("In Level 1 finally"); } }}
運行:
Finally方法必定運行。
總結:當有多層嵌套的finally時,異常在不同的層次拋出 ,在不同的位置拋出,可能會導致不同的finally語句塊執行順序。
6.通過SystemExitAndFinally.java樣本程式驗證finally語句塊是否一定會執行。
代碼:
public class SystemExitAndFinally { public static void main(String[] args) { try{ System.out.println("in main"); throw new Exception("Exception is thrown in main"); //System.exit(0);} catch(Exception e) { System.out.println(e.getMessage()); System.exit(0); } finally {System.out.println("in finally");} }}
運行結果:
由此,finally語句塊一定會執行。
7.編寫一個程式,此程式在運行時要求使用者輸入一個整數,代表某門課的考試成績,程式接著給出“不及格”、“及格”、“中”、“良”、“優”的結論。
代碼;
package boke1125; import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Scanner; public class Grade { public static void main(String[] args) {while(true){//判斷整數//判斷成績等級 try { BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); //拋出受控的異常 System.out.println("請輸入考試成績:(正整數)"); int grade = Integer.parseInt(buf.readLine()); //有可能引發運行時異常 System.out.println("成績為: "+grade); if(grade<0||grade>100){ MyException1 e1=new MyException1("成績應在0~100之間!"); throw e1; } if(grade>=0&&grade<60){ System.out.println("你處於不及格水平"); break; } else if(grade>=60&&grade<70){ System.out.println("你處於及格水平"); break; } else if(grade>=70&&grade<80){ System.out.println("你處於中等水平"); break; } else if(grade>=80&&grade<90){ System.out.println("你處於良好水平"); break; } else if(grade>=90&&grade<=100){ System.out.println("你處於優秀水平"); break; } } //以下異常處理語句塊是必須的,否則無法通過編譯 catch(MyException1 e1) { System.out.println(e1); } catch(IOException e) { System.out.println("I/O錯誤"); } //以下異常處理語句塊可以省略,不影響編譯,但在運行時出錯 catch(NumberFormatException e) { System.out.println("成績必須為整數"); }}} } class MyException1 extends Exception{ public MyException1(String str){super(str);}}
運行結果:
Java有關異常處理的小程式