java 動手動腦7

---恢復內容開始---java

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

閱讀如下代碼(CatchWho.java),寫出程序運行結果:3d

ArrayIndexOutOfBoundsException/內層try-catchcode

發生ArithmeticExceptionblog

 

1、源碼:get

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程序運行的結果it

ArrayIndexOutOfBoundsException/外層try-catchio

一、源代碼class

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

 

 

2、當有多個嵌套的trycatchfinally時,要特別注意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語句塊執行順序。

 

 

3、辨析: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()則不能執行。

 

4、編寫一個程序,此程序在運行時要求用戶輸入一個 整數,表明某門課的考試成績,程序接着給出「不及格」、「及格」、「中」、「良」、「優」的結論。

要求程序必須具有足夠的健壯性,無論用戶輸入什 麼樣的內容,都不會崩潰。

一、源代碼

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你不及格哎!");}            
    }
}

 

---恢復內容結束---

相關文章
相關標籤/搜索