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

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

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

finally {
  無論什麼狀況都會執行,包括try catch 裏面用了return ,能夠理解爲只要執行了try或者catch,就必定會執行 finally
}code

看下面題目對比:blog

 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 }

結果爲:get

CGACGio

 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 }

結果爲:class

i不能爲1
ACGMCtest

2、throw和throws的區別

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

 

 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,不符合要求");
        }
    }
}
相關文章
相關標籤/搜索