throws关键字 声明,使用throws声明的方法是表示此方法不处理异常,而是交给方法的调用处进行处理,
throw关键字可以人为的抛出一个异常,抛出异常时,直接抛出异常类的实例化对象即可。
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;是进行异常的处理。
throw和throws:只是进行异常的抛出。