java异常处理throw和throws的区别
throws是用来声明一个方法可能抛出的异常信息
,声明但不处理,出现异常他会往上传递异常,谁调用这个方法谁处理这个异常
class Math{
2 public int div(int i,int j) throws Exception{
3 int t=i/j;
4 return t;
5 }
6 }
throw就是自己处理异常
,自己捕获并处理异常(try catch)
public static void main(String[] args)
4 {
5 try
6 {
9 throwChecked(-3);
10 }
11 catch (Exception e)
12 {
13 System.out.println(e.getMessage());
14 }
17 throwRuntime(3);
18 }