throws与throw关键字详解
throws关键字
throws关键字用在方法声明上,明确告诉调用者本方法可能产生的的异常,但方法本身不处理,用throws向上层抛出。
//throws关键字的简单使用
public class Test{
public static void main(String[] args) {
try{
System.out.println(print(10,0));
}catch (Exception e){
//程序中出现错误的所有具体异常均是由Exception继承而来,e为Exception类的一个对象,该类对象由IVM产生,不需new就可直接使用
e.printStackTrace();
}
}
public static int print(int x,int y)throws Exception{
//在方法声明上使用,但不做任何处理,将异常抛回上层
return x/y;
}
}
那么其他类若想调用throws声明的方法时该怎么办呢?
//调用的正确使用
public <