您的位置:首页 > 编程语言 > Java开发

【Java学习】Java异常-Try、Throws、Throw关键字理解

2016-07-29 16:50 676 查看

Try和Throws

try:针对于可以会发生异常的语句,不管以后会不会真的发生异常,程序都会做好防护准备,防范于未然。

throws:用于一个方法的声明,表示该方法不处理异常;同样对于可能会发生异常的语句,但是现在并没有发生异常,现在先不处理,等以后真正使用的时候再说吧!就相当于将 自己代码块的功能与异常处理的部分分层了,并且不去管异常处理的部分,回想一下倒是做到了程序的低耦合啊!(总是觉得挺不负责的)

也可以这样理解:一般方法抛出异常,那么捕获异常的工作就该是调用该方法的代码块去做,一般是main()方法,那如果继续在main()方法上抛出异常,会怎样呢?现在我只能确定main()方法中不用捕获异常了。那么具体发生异常的时候会是怎么样?下面代码来测试

code 1 在main()方法上抛出异常

public class TestException {
public static int Test(int a,int b)throws Exception{
int result=a/b;
return  result;
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
System.out.println(Test(10,0));
}
}


结果:



code2 在main()方法中捕获异常

public class TestException {
public static int Test(int a,int b)throws Exception{
int result=a/b;
return  result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
System.out.println(Test(10,0));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}


结果:



观察上述两种代码的结果,感觉结果差不多,只是在main()方法上抛出异常的代码运行结果 中会多出一行“Exception in Thread main:”,这代表着什么?后来我查了一些资料,发现,在main()方法上抛出异常,就是将异常交由JVM处理了,如果在main()方法中捕获异常,然后在catch代码块中做适当处理,程序还可以继续执行,然而如果将异常交由JVM处理,一旦真的发生了异常,那么程序将只能中断!

Throw

手动地抛出异常,<个人理解>:throw应该主要会用于自定义异常类,语法:throw new ExceptionName(String msg),表示已经触发某条件,程序将会进入某自定义异常,然而有异常发生应该捕获异常,所以使用try catch捕获一下,但是回过头想了一下,我自己手动抛出异常,表示我知道这个条件必然会发生异常,然而在自己还去捕获它,为什么不直接做这个条件的后续处理而是要使用异常先捕获了再去做后续的处理?总之我是觉得中间的抛出和捕获异常有些画蛇添足了.

看下面的代码:

code3

public class TestException {
public static int Test(int a, int b) {
if (b == 0) {
try {
//手动抛出异常
throw new Exception("除数不可以为0");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int result = a / b;
return result;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(Test(10, 0));
}

}


看上面的代码,自己抛异常,然后自己捕获异常,感觉挺无语的,这不是自欺欺人吗?我们是不是可以省去中间异常的过程,直接写处理过程,代码如下

code4

public class TestException {
public static int Test(int a, int b) {
if (b == 0) {
//          try {
//              //手动抛出异常
//              throw new Exception("除数不可以为0");
//          } catch (Exception e) {
//              // TODO Auto-generated catch block
//              e.printStackTrace();
//          }
System.out.println("异常:除数不可以为0");
System.exit(0);
}
int result = a / b;
return result;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(Test(10, 0));
}

}


如果像上面的代码一样,在同一个方法里抛出异常同时又取捕获他,确实很怪异,但是如果仅仅抛出异常而先不做处理会有什么不同吗?先反思上面的例子,我们使用throw的好处是可以针对特定的异常,仅仅是除数不可以为0,其他的异常都不去管,缺点是,后续的处理就没了,程序捕获异常后就直接中断了;所以我们尝试在main()方法中捕获异常并作更多的后续处理看看代码

code5

public class TestException {
public static int Test(int a, int b) throws Exception {
if (b == 0) {
throw new Exception("除数不可以为0");
}
int result = a / b;
return result;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
try {
System.out.println(Test(10, 0));
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
System.out.println("程序还在继续~~~~~~~~");
}
}
}


结果如下



看起来确实是这样,throw关键字可以针对于特定的异常或者说条件,当遇到这些特定异常或条件时,做出和捕获一般异常类似的处理。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 异常 异常处理