java异常处理的throw和throws的区别

  1. 区别

throws是用来声明一个方法可能抛出的所有异常信息,throws是将异常声明但是不处理,而是将异常往上传,谁调用我就交给谁处理。而throw则是指抛出的一个具体的异常类型。

2.分别介绍

throws:用于声明异常,例如,如果一个方法里面不想有任何的异常处理,则在没有任何代码进行异常处理的时候,必须对这个方法进行声明有可能产生的所有异常(其实就是,不想自己处理,那就交给别人吧,告诉别人我会出现什么异常,报自己的错,让别人处理去吧)。

格式是:方法名(参数)throws 异常类1,异常类2,.....

复制代码
1 class Math{
2 public int div(int i,int j) throws Exception{
3 int t=i/j;
4 return t;
5 }
6 }
7
8 public class ThrowsDemo {
9 public static void main(String args[]) throws Exception{
10 Math m=new Math();
11 System.out.println("出发操作:"+m.div(10,2));
12 }
13 }
复制代码

throw:就是自己进行异常处理,处理的时候有两种方式,要么自己捕获异常(也就是try catch进行捕捉),要么声明抛出一个异常(就是throws 异常~~)。

注意:

throw一旦进入被执行,程序立即会转入异常处理阶段,后面的语句就不再执行,而且所在的方法不再返回有意义的值!

复制代码
1 public class TestThrow
2 {
3 public static void main(String[] args)
4 {
5 try
6 {
7 //调用带throws声明的方法,必须显式捕获该异常
8 //否则,必须在main方法中再次声明抛出
9 throwChecked(-3);
10 }
11 catch (Exception e)
12 {
13 System.out.println(e.getMessage());
14 }
15 //调用抛出Runtime异常的方法既可以显式捕获该异常,
16 //也可不理会该异常
17 throwRuntime(3);
18 }
19 public static void throwChecked(int a)throws Exception
20 {
21 if (a > 0)
22 {
23 //自行抛出Exception异常
24 //该代码必须处于try块里,或处于带throws声明的方法中
25 throw new Exception("a的值大于0,不符合要求");
26 }
27 }
28 public static void throwRuntime(int a)
29 {
30 if (a > 0)
31 {
32 //自行抛出RuntimeException异常,既可以显式捕获该异常
33 //也可完全不理会该异常,把该异常交给该方法调用者处理
34 throw new RuntimeException("a的值大于0,不符合要求");
35 }
36 }
37 }

实事求是,不自以为是

上一篇:8.21 [JavaSE] 异常.File类


下一篇:理解JAVA中的throw关键字