深入理解try catch吃掉异常,及catch(Exception e)中的异常
来源:互联网 发布:数据统计图表分析 编辑:程序博客网 时间:2024/06/05 06:58
package test.s;public class yichang {public static void main(String[] args) throws Exception{try{double a=aa();System.out.println(a);}catch(Exception e){e.printStackTrace();}}public static double aa() throws Exception{double b = 0;try{b=1/0;}catch(Exception e){throw new Exception(e.getMessage());}return b;}}
输出:
java.lang.Exception: / by zeroat test.s.yichang.aa(yichang.java:18)at test.s.yichang.main(yichang.java:6)
说明:这算是比较正常的异常写法。aa()方法抛出异常,mian方法捕获异常,并打印出异常原因。
2,
package test.s;public class yichang {public static void main(String[] args) throws Exception{try{double a=aa();System.out.println(a);}catch(Exception e){}}public static double aa() throws Exception{double b = 0;try{b=1/0;}catch(Exception e){throw new Exception(e.getMessage());}return b;}}
没有输出;
说明:这个跟1的区别是main方法捕获aa传来的异常后没有将异常打印出来,所以没有任何输出。
3,
package test.s;public class yichang {public static void main(String[] args) throws Exception{try{double a=aa();System.out.println(a);}catch(NullPointerException e){e.printStackTrace();}}public static double aa() throws Exception{double b = 0;try{b=1/0;}catch(Exception e){throw new Exception(e.getMessage());}return b;}}输出:
Exception in thread "main" java.lang.Exception: / by zeroat test.s.yichang.aa(yichang.java:18)at test.s.yichang.main(yichang.java:6)
说明:在主方法中的catch(nullPointerException e)是空指针异常。而aa()方法抛出来的异常是ArithmeticException,所以main方法虽然用try catch把aa()方法包裹起来,但是并没有捕获改异常。控制台打印的是java自己处理打印出来的异常。
效果跟下面的代码是一样的:也就是main方法中不用try catch
package test.s;public class yichang {public static void main(String[] args) throws Exception{double a=aa();System.out.println(a);}public static double aa() throws Exception{double b = 0;try{b=1/0;}catch(Exception e){throw new Exception(e.getMessage());}return b;}}
4,
package test.s;public class yichang {public static void main(String[] args) throws Exception{try{double a=aa();System.out.println(a);}catch(NullPointerException e){e.printStackTrace();}}public static double aa() throws Exception{double b = 0;try{b=1/0;}catch(NullPointerException e){throw new NullPointerException(e.getMessage());}return b;}}
输出:
Exception in thread "main" java.lang.ArithmeticException: / by zeroat test.s.yichang.aa(yichang.java:16)at test.s.yichang.main(yichang.java:6)
说明这种是catch(NullPointerException e),在aa方法中只能捕获空指针异常,但是b=1/0报的是算术异常,因此也是无法捕获的。使用debug跑程序会发现程序运到b=1/0就打印异常结束程序了。
因此同以下代码:
package test.s;public class yichang {public static void main(String[] args){double a=aa();System.out.println(a);}public static double aa() {double b = 0;b=1/0;return b;}}
5,
package test.s;public class yichang {public static void main(String[] args) throws Exception{try{double a=aa();System.out.println(a);}catch(NullPointerException e){e.printStackTrace();}}public static double aa() throws Exception{double b = 0;try{b=1/0;}catch(ArithmeticException e){throw new ArithmeticException(e.getMessage());}return b;}}
输出:
Exception in thread "main" java.lang.ArithmeticException: / by zeroat test.s.yichang.aa(yichang.java:18)at test.s.yichang.main(yichang.java:6)
6,最准确的情况
package test.s;public class yichang {public static void main(String[] args) throws Exception{try{double a=aa();System.out.println(a);}catch(ArithmeticException e){e.printStackTrace();}}public static double aa() throws Exception{double b = 0;try{b=1/0;}catch(ArithmeticException e){throw new ArithmeticException(e.getMessage());}return b;}}
输出:
java.lang.ArithmeticException: / by zeroat test.s.yichang.aa(yichang.java:18)at test.s.yichang.main(yichang.java:6)
说明:因为知道aa方法抛出的异常是ArithmeticException,所以准确定位第一层异常捕获。然后在main方法中也精确捕获到aa方法抛来的算术异常。
总结,正确使用try catch 异常,try 不是能吃掉所有的异常,必须要在catch中使用正确的异常才能捕获。但是在实际开发中,很难精确的捕获可能存在的异常。因此我们大多使用第一种情况,exception是所有异常的父类,能捕获到所有的异常。
阅读全文
0 0
- 深入理解try catch吃掉异常,及catch(Exception e)中的异常
- try{}catch(Exception e){}异常处理
- java中的异常(exception)&&try,catch,finally throw&&throws
- PHP中的exception异常处理中的Try{}catch{}讲解
- JS中的异常处理(try.catch)
- exception(异常处理)与try,catch,
- java基础之异常Exception---try...catch
- java try catch 吃掉异常与跑出异常的区别,以及在项目中是否改抛出异常的理解
- try catch(CException *e)捕获异常
- JavaScript 异常处理Try{}catch(e){}
- 异常(try-throw-catch)
- 异常(try-catch-finally)
- 我的笔记 异常处理try{} catch(Exception e){} throws Exception
- java中的异常处理 try-catch 以及try-catch-finally
- try catch 异常处理
- try catch捕获异常
- JAVA 异常 try-catch
- try-catch异常处理
- jquery 制作出右键菜单
- 修改Xcode自动生成的文件注释来导出API文档
- Crypto++
- Vue2.0使用props传递数据【data篇】
- 【机器学习】Random Forests随机森林的基础及运用
- 深入理解try catch吃掉异常,及catch(Exception e)中的异常
- RabbitMQ安装详解
- js中set与map
- 段寄存器和8种地址寻址方式
- 不能显示日志Failed to load class "org.slf4j.impl.StaticLoggerBinder".
- python print格式化输出
- 自动转换静态表格(php+ mysql)
- 在UICollectionView上面添加分割线
- 让浮层水平垂直居中的方法