java中異常的處理

   throws關鍵字 聲明,使用throws聲明的方法是表示此方法不處理異常,而是交給方法的調用處進行處理,java

   throw關鍵字能夠人爲的拋出一個異常,拋出異常時,直接拋出異常類的實例化對象便可。spa

class Math
{
public int div(int i,int j) throws Exception
{
try{
int temp=i/j;
return temp;
}catch(Exception e)
{
throw e;
}
retrun temp;
}
}

public class ThrowsDemo1
{
public static void main(String arg[])
{
Math m=new Math();
try
{
System.out.println("除法操做:"+m.div(10,2));
}catch(Exception e)
{
e.printStackTrace();
}
}
}

try...catch;是進行異常的處理。code

throw和throws:只是進行異常的拋出。對象

相關文章
相關標籤/搜索