throws与throw区别

1.throws 用于抛出方法层次的异常, 并且直接由些方法调用异常处理类来处理该异常, 所以它常用在方法的后面。比如 public static void main(String[] args) throws SQLException

 

2.throw 用于方法块里面的代码,比throws的层次要低,比如try...catch ....语句块,表示它抛出异常, 但它不会处理它, 而是由方法块的throws Exception来调用异常处理类来处理。

 

throw用在程序中,明确表示这里抛出一个异常。   throws用在方法声明的地方,表示这个方法可能会抛出某异常。

 

throw是抛出一个具体的异常类,产生一个异常。throws则是在方法名后标出该方法会产生何种异常需要方法的使用者捕获并处理。

 

 

1、throws关键字通常被应用在声明方法时,用来指定可能抛出的异常。

多个异常可以使用逗号隔开。当在主函数中调用该方法时,如果发生异常,就会将异常抛给指定异常对象。如下面例子所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
public  class  Shoot {   创建类
static  void  pop()  throws  NegativeArraySizeException {
//定义方法并抛出NegativeArraySizeException异常
int  [] arr =  new  int [- 3 ]; //创建数组
}
public  static  void  main(String[] args) { //主方法
try 
pop();  //调用pop()方法
catch  (NegativeArraySizeException e) {
System.out.println( "pop()方法抛出的异常" ); //输出异常信息
}
}
}

2、throw关键字通常用在方法体中,并且抛出一个异常对象。程序在执行到throw语句时立即停止,它后面的语句都不执行。

通过throw抛出异常后,如果想在上一级代码中来捕获并处理异常,则需要在抛出异常的方法中使用throws关键字在方法声明中指明要跑出的异常;如果要捕捉throw抛出的异常,则必须使用try—catch语句。举例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class  MyException  extends  Exception {  //创建自定义异常类
String message;  //定义String类型变量
public  MyException(String ErrorMessagr) {   //父类方法
       message = ErrorMessagr;
}
public  String getMessage(){    //覆盖getMessage()方法
  return  message;
}
}
public  class  Captor {  //创建类
static  int  quotient( int  x, int  y)  throws  MyException{ //定义方法抛出异常
if (y <  0 ){   //判断参数是否小于0
         throw  new  MyException( "除数不能是负数" ); //异常信息
}
return  x/y; //返回值
}
public  static  void  main(String args[]){  //主方法
try //try语句包含可能发生异常的语句
                int  result = quotient( 3 ,- 1 ); //调用方法quotient()
    } catch  (MyException e) {  //处理自定义异常
  System.out.println(e.getMessage());  //输出异常信息
  }
    catch  (ArithmeticException e) {
                   //处理ArithmeticException异常
  System.out.println( "除数不能为0" ); //输出提示信息
  }
   catch  (Exception e) {  //处理其他异常
  System.out.println( "程序发生了其他的异常" );
                  //输出提示信息
  }
}
}

猜你喜欢

转载自jiongs.iteye.com/blog/2358795
今日推荐