Java中 try--catch-- finally、throw、throws 的用法


一、try {..} catch {..}finally {..}用法

try {
  執行的代碼,其中可能有異常。一旦發現異常,則立即跳到catch執行。否則不會執行catch里面的內容
} catch (Exception e) {
  除非try里面執行代碼發生了異常,否則這里的代碼不會執行
}

finally {
  不管什么情況都會執行,包括try catch 里面用了return ,可以理解為只要執行了try或者catch,就一定會執行 finally
}

看下面題目對比:

 1 public class test1 {
 2     public static String output="";
 3     public static void foo(int i) {
 4         try {
 5             if(i==1) //throw new  Exception("i不能為1");
 6             output+="A";            
 7         } catch (Exception e) {
 8             System.out.println(e.getMessage());
 9             output+="M";
10             return;
11         }finally {
12             output+="C";            
13         }
14         output+="G";
15         
16     }
17     public static void main(String[] args) {
18         foo(0);
19         foo(1);
20         System.out.println(output);
21     }
22 }

結果為:

CGACG

 1 public class test1 {
 2     public static String output="";
 3     public static void foo(int i) {
 4         try {
 5             
 6         } catch (Exception e) {
 7             // TODO: handle exception
 8         }finally {
 9             
10         }
11         try {
12             if(i==1) throw new  Exception("i不能為1");
13             output+="A";            
14         } catch (Exception e) {
15             System.out.println(e.getMessage());
16             output+="M";
17             return;
18         }finally {
19             output+="C";            
20         }
21         output+="G";
22         
23     }
24     public static void main(String[] args) {
25         foo(0);
26         foo(1);
27         System.out.println(output);
28     }
29     
30 }

結果為:

i不能為1
ACGMC

二、throw和throws的區別

   throws:用於聲明異常,例如,如果一個方法里面不想有任何的異常處理,則在沒有任何代碼進行異常處理的時候,必須對這個方法進行聲明有可能產生的所有異常(其實就是,不想自己處理,那就交給別人吧,告訴別人我會出現什么異常,報自己的錯,讓別人處理去吧)。格式是:方法名(參數)throws 異常類1,異常類2,.....

 

 1 class Math{
 2       public int div(int i,int j) throws Exception{
 3           int t=i/j;
 4           return t;
 5       }
 6  }
 7 
 8 public class ThrowsDemo {
 9       public static void main(String args[]) throws Exception{
10           Math m=new Math();
11           System.out.println("出發操作:"+m.div(10,2));
12      }
13  }

 

  throw:就是自己進行異常處理,處理的時候有兩種方式,要么自己捕獲異常(也就是try catch進行捕捉),要么聲明拋出一個異常(就是throws 異常~~)。

  注意:

    throw一旦進入被執行,程序立即會轉入異常處理階段,后面的語句就不再執行,而且所在的方法不再返回有意義的值!

public class TestThrow
{
    public static void main(String[] args) 
    {
        try
        {
            //調用帶throws聲明的方法,必須顯式捕獲該異常
            //否則,必須在main方法中再次聲明拋出
            throwChecked(-3);            
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
        //調用拋出Runtime異常的方法既可以顯式捕獲該異常,
        //也可不理會該異常
        throwRuntime(3);
    }
    public static void throwChecked(int a)throws Exception
    {
        if (a > 0)
        {
            //自行拋出Exception異常
            //該代碼必須處於try塊里,或處於帶throws聲明的方法中
            throw new Exception("a的值大於0,不符合要求");
        }
    }
    public static void throwRuntime(int a)
    {
        if (a > 0)
        {
            //自行拋出RuntimeException異常,既可以顯式捕獲該異常
            //也可完全不理會該異常,把該異常交給該方法調用者處理
            throw new RuntimeException("a的值大於0,不符合要求");
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM