1. 程式人生 > >動手動腦之異常處理

動手動腦之異常處理

整型 位置 time 必須 動手 異常 who ice int

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

技術分享

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

技術分享

3.閱讀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");
        
        }
    
    }

}

技術分享

原因:當有多個嵌套的try…catch…finally時,異常在不同位置被接受,可能會導致異常下面的finally語句塊執行順序。不管是否有異常發生,finally語句塊中的語句始終保證被執行。

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

不會,System.exit(0)可以終止程序,finally語句塊一定會執行。

5.自行歸納Java多層嵌套異常處理的基本流程

在java語言中,通常將可能出現異常的語句放入try{}語句中,將出現錯誤後需要執行的語句放入到catch{}語句中,將無論是否發生異常都要執行的語句放在finally{}語句中。當程序執行出現異常的時候,系統會拋出一個異常,然後由try{}語句中中出現異常的地方轉到catch{}語句中。不過不管有沒有異常產生,finally{}中的語句都將執行。如果系統出現系統錯誤或者運行Runtime異常,jvm會結束程序運行,不一定會執行finally{}中的語句。如果try{}中產生的異常在catch中沒有處理,系統將停止程序,也不會執行finally中的語句。

6.動手動腦,編寫一個程序,此程序在運行時要求用戶輸入一個 整數,代表某門課的考試成績,程序接著給出“不及格”、“及格”、“中”、“良”、“優”的結論。要求程序必須具備足夠的健壯性,不管用戶輸入什 麽樣的內容,都不會崩潰。

import java.util.Scanner;
public class Test 
{
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);
        System.out.println("請輸入分數:");

        String score=input.nextLine();
        while(true)
        {
        try
        {
            for(int i=0;i<score.length();i++)
            {
                
                if(!(score.charAt(i)>=48&&score.charAt(i)<=57))
                {
                    throw new MyException();
                }
            }
            int m = Integer.parseInt(score);//把字符串轉換成整型
            String str="";
            if(m<60)
            {
                str="不及格";
            }
            else if(m<70)
            {
                str="及格";
            }
            else if(m<80)
            {
                str="中";
            }
            else if(m<90)
            {
                str="良";
            }
            else if(m<=100)
            {
                str="優";
            }
            else if (m>100||m<0)
            {
                throw new MyException();
            }
            System.out.println("分數等級為:"+str);    
            break;
        }
        catch(MyException A)
        { 
             A.error();
        }

        }
    }
}
class  MyException extends Exception{

    public void error()
    {
            System.out.println("你輸入的數字不符合要求");    

    }
}

技術分享

動手動腦之異常處理