【Java学习】Java异常-Try、Throws、Throw关键字理解
来源:互联网 发布:苹果电脑编程 编辑:程序博客网 时间:2024/09/21 09:21
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学习】Java异常-Try、Throws、Throw关键字理解
- Java异常机制try、catch、finally、throw、throws五个关键字。
- Java异常处理throws,throw关键字try,catch,finally模块
- JAVA异常处理关键字:throws,throw,try,catch,finally
- 关于java异常处理的几个关键字 try catch/throw/throws
- 关于java异常处理的几个关键字 try catch/throw/throws
- 黑马程序员——Java中的异常:关键字try...catch、throw、throws总结
- JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意思?
- 关于java异常处理的几个关键字 try catch/throw/throws
- Java语言如何进行异常处理,关键字:throws、throw、try、catch、finally分别如何使用?
- JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意义?
- Java语言中进行异常处理的关键字具体含义:throws、throw、try、catch、finally
- Java.异常:自定义异常类,throw,throws,try...catch,finally
- java throw throws try catch
- java异常处理之throw, throws,try和catch
- java异常处理之throw, throws,try和catch
- Java初学习-异常处理 throw, throws,try和catch
- java异常处理之throw, throws,try和catch
- 由浅入深:自己动手开发模板引擎——置换型模板引擎(三)
- RPC相关知识
- css3 flex
- Android 6.0 需要在运行时请求权限
- Spring安全框架 Spring Security
- 【Java学习】Java异常-Try、Throws、Throw关键字理解
- bootstrap-table 筛选数据报400错误
- Loadrunner测试webservice接口
- 杭电-2553 N皇后问题 (经典深搜)
- 【SQL】——Oracle之ROWNUM
- 由浅入深:自己动手开发模板引擎——置换型模板引擎(四)
- HTML5 canvas双缓存实例
- hibernate 性能优化笔记
- GsonFormat快速实现JavaBean