1. 程式人生 > >課堂動手動腦驗證以及自定義異常類實現對異常處理——java異常類

課堂動手動腦驗證以及自定義異常類實現對異常處理——java異常類

異常(exception):發生在程式執行期間,表明出現了一個非法執行的情況。許多JDK中的方法在檢測到非法情況時,都會丟擲一個異常物件。例如:陣列越界和被0除。

程式碼驗證:

package test;

import javax.swing.*;

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

輸出結果:

 

當java程式中出現多try catch的情況時,一定要注意程式執行的先後順序。

多try catch的java異常處理程式碼一

package test;

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

程式執行結果:

多try catch的java異常處理程式碼二

package test;

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的執行時機。特別注意:當有多層巢狀的finally時,異常在不同的層次丟擲,在不同的位置丟擲,可能會導致不同的finally語句的執行順序。再者,我們一般認為finally語句中的句子一定會被執行,這裡的一定是相對而言的,並不是絕對的。例如以下程式程式碼的執行:

package test;

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


}

執行結果:

在這段java程式碼中finally語句塊並沒有執行。

 

通過過異常的學習,自己嘗試了自定義了一個異常類來處理異常,原始碼如下:

package classtest;

import java.util.Scanner;

class Myexception extends Exception {
    public Myexception(String message) {
        super(message);
    }
}

public class Mytest {

    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub
        try{
            function();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println("main()函式執行時出現異常");
        }
        finally {
            System.out.println("main()函式執行完畢");
        }
    }

    public static int judge(String str) {
        int c = 0;
        String regex = "[0-9]+";
        String regex1="[-][0-9]+";
        boolean d = str.matches(regex);
        boolean e = str.matches(regex1);
        if (d == false) {
            c = 1;
            if (e == true) {
                c = 0;
            }
        }
        return c;
    }

    public static void function() {
        try {
            System.out.println("請輸入一個正整數:");
            Scanner input = new Scanner(System.in);
            String a = input.next();
            int temp = judge(a);
            if (temp == 1) {
                throw new Myexception("不能輸入非法字元");
            } else {
                int num = Integer.parseInt(a);
                if (num < 0)
                    throw new Myexception("輸入不能為負數");
                else if (num == 0) {
                    throw new Myexception("正整數不包括0");
                }
            }

        } catch (Myexception f) {
            System.out.println(f.getMessage());
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println("function()函式執行時出現異常");
        }
       finally {
           System.out.println("function()函式已執行完畢,請指示下一步動作");
       }
        
    }
}

 

&n