throw与throws的区别
1.throw是在方法内部的代码块中,手动异常抛出
2.throws是在方法定义时用
举例如下
1、手动抛出–throw
public class Hello {
public static void main(String[] args) {
try {
throw new Exception("手动抛出的异常");
}catch(Exception e) {
e.printStackTrace();
}
}
}
2、方法定义时–throws
class Text{
public static int div(int x,int y) throws Exception{
return x/y;
}
}
public class Hello {
public static void main(String[] args) {
try {
System.out.println(Text.div(12, 0));
}catch(Exception e) {
e.printStackTrace();
}
}
}
来源:https://blog.csdn.net/weixin_39537054/article/details/100144384