java-10異常處理動手動腦

來源:互聯網
上載者:User

標籤:樣本   錯誤處理   static   實現   函數   http   閱讀   stack   exce   

1.請閱讀並運行AboutException.java樣本,然後通過後面的幾頁PPT瞭解Java中實現異常處理的基礎知識。

import javax.swing.*;class AboutException {   public static void main(String[] a)    {      int i=1, j=0, k;      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");     }          }}

把可能會發生錯誤的代碼放進try語句塊中。 當程式檢測到出現了一個錯誤時會拋出一個異常對象。異常處理代碼會捕獲並處理這個錯誤。 catch語句塊中的代碼用於處理錯誤。 當異常發生時,程式控制流程程由try語句塊跳轉到catch語句塊。 不管是否有異常發生,finally語句塊中的語句始終保證被執行。

2.閱讀以下代碼(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");         }     } }

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

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

當有多層嵌套的finally時,異常在不同的層次拋出 ,在不同的位置拋出,可能會導致不同的finally語句塊執行順序。

3.辨析: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語句不一定執行

4.如何跟蹤異常的傳播路徑?

// UsingExceptions.java// Demonstrating the getMessage and printStackTrace// methods inherited into all exception classes.public class PrintExceptionStack {   public static void main( String args[] )   {      try {         method1();      }      catch ( Exception e ) {         System.err.println( e.getMessage() + "\n" );         e.printStackTrace();      }   }   public static void method1() throws Exception   {      method2();   }   public static void method2() throws Exception   {      method3();   }   public static void method3() throws Exception   {      throw new Exception( "Exception thrown in method3" );   }}

 

當程式中出現異常時,JVM會依據方法調用順序依次尋找有關的錯誤處理程式。

每個Throwable類的對象都有一個getMessage方法,它返回一個字串,這個字串是在Exception建構函式中傳入的,通常讓這一字串包含特定異常的相關資訊。

5.成績:

import java.util.Scanner;class judgeException extends Exception{    public judgeException(String s){        super(s);    }}class judgeException2 extends Exception{    public judgeException2(String s){    super(s);    }}public class Test {     public static void main(String[] args) {        boolean p=true;        while(p){        System.out.println("請輸入學生的分數:");        Scanner in=new Scanner(System.in);        String s=in.next();        try{            for(int i=0;i<s.length();i++){                if((s.charAt(i)<48||s.charAt(i)>57)){                    throw new judgeException("輸入錯誤,請輸入一個數字:");                }            }            try{                int m=Integer.parseInt(s);                if(m<0||m>100){                    throw new judgeException2("請輸入正確的分數");                }                if(m<60){                    System.out.println("不及格");                }                else if(m>=60&&m<=69){                    System.out.println("及格");                }                else if(m>=70&&m<=79){                    System.out.println("中");                }                else if(m>=80&&m<=89){                    System.out.println("良");                }                else if (m>=90&&m<=100){                    System.out.println("優");                }            }            catch(judgeException2 e){                System.out.println(e.getMessage());            }                         }        catch (judgeException e){            System.out.println(e.getMessage());        }          }    }}

java-10異常處理動手動腦

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.