java 動手動腦7

來源:互聯網
上載者:User

標籤:finally   ati   can   level   embed   嵌套   nbsp   編寫   tcl   

---恢複內容開始---

一、動手動腦:多層的異常捕獲-1

閱讀以下代碼(CatchWho.java),寫出程式運行結果:

ArrayIndexOutOfBoundsException/內層try-catch

發生ArithmeticException

 

1、源碼:

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

動手動腦:多層的異常捕獲-2

寫出CatchWho2.java程式啟動並執行結果

ArrayIndexOutOfBoundsException/外層try-catch

1、原始碼

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

 

 

二、當有多個嵌套的try…catch…finally時,要特別注意finally的執行時機。

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語句塊一定會執行嗎?

SystemExitAndFinally.java樣本

代碼:

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

總結:無論catch()語句有沒有運行finally()語句都會執行,但如果層序的前面有Syatem.exit(1);finally()則不能執行。

 

四、編寫一個程式,此程式在運行時要求使用者輸入一個 整數,代表某門課的考試成績,程式接著給出“不及格”、“及格”、“中”、“良”、“優”的結論。

要求程式必須具備足夠的健壯性,不管使用者輸入什 麼樣的內容,都不會崩潰。

1、原始碼

import java.util.Scanner;public class Grade{    public static void main(String[] args)    {        Scanner in=new Scanner(System.in);        System.out.println("請輸入學產生績:");        String sub=in.next();        for(int i=0;i<sub.length();i++)        {            if(sub.charAt(i)>=57||sub.charAt(i)<=48)try {throw new   Exception(sub);}                             catch (Exception e) {                System.out.println("輸入錯誤,請輸入學產生績:");                sub=in.next();                }        }        if(Integer.parseInt(sub)>=90&&Integer.parseInt(sub)<=100)        {System.out.println("該學產生績為:"+sub+"\n優秀呢!");}        if(Integer.parseInt(sub)>=80&&Integer.parseInt(sub)<90)        {System.out.println("該學產生績為:"+sub+"\n良等!");}        if(Integer.parseInt(sub)>=70&&Integer.parseInt(sub)<80)        {System.out.println("該學產生績為:"+sub+"\n中等!");}        if(Integer.parseInt(sub)>=60&&Integer.parseInt(sub)<70)        {System.out.println("該學產生績為:"+sub+"\n及格嘍!");}        if(Integer.parseInt(sub)>=0&&Integer.parseInt(sub)<60)        {System.out.println("該學產生績為:"+sub+"\n你不及格哎!");}                }}

 

---恢複內容結束---

java 動手動腦7

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.