java 异常总结
来源:互联网 发布:蓝光软件 编辑:程序博客网 时间:2024/06/05 19:10
1.异常的分类
从上面的异常的类图中,我们可以看出:
- Thorwable类所有异常和错误的超类,有两个子类Error和Exception,分别表示错误和异常。
- 异常类Exception又分为运行时异(RuntimeException)和非运行时异常,也称之为不检查异常(Unchecked Exception)和检查异常(Checked Exception)
几点说明:
1.运行时异常都是RuntimeException类及其子类异常,如NullPointerException、IndexOutOfBoundsException等,这些异常是不检查异常,程序中可以选择捕获处理,也可以不处理。这些异常一般是由程序逻辑错误引起的,程序应该从逻辑角度尽可能避免这类异常的发生。
以最常见的运行时异常NullPointerException为例,如果在你的代码中遇到该异常,可以不做处理,或者try catch捕获。
//①不做任何处理public class test { public static void main(String[] args) { getFile(); } public static void getFile(){ throw new NullPointerException("空指针异常"); }}
//②捕获异常public class test { public static void main(String[] args) { getFile(); } public static void getFile(){ String path = null; try { File file = new File(path); } catch (Exception e) { System.out.println("文件路径不能为空!"); } }}
运行时异常(也就是继承于RuntimeException类的异常)在Java语法中是unchecked的,也就是允许程序不用try…catch….或者throws去捕获或者声明抛出,因为“运行时”的异常在逻辑上被定义为那种在运行时根据各种复杂的程序环境可能造成的异常,而且往往无法判断在异常发生后应该如何处理,这是,编译器就不强迫用户去写一个try…catch…去捕获或者throws异常了,因为用户既不知道异常是不是会发生,也不一定知道异常发生了该怎么处理。
也就是说,如果不是runtimeException异常,则必须处理,处理方式:①抛出②try…catch
//方式①抛出 (方法体抛出) public void exceptionDemo() throws MyException{ throw new MyException("测试异常..."); } //方式②捕获 public void exceptionDemo(){ try { throw new MyException("测试异常..."); } catch (MyException e) { e.printStackTrace(); } } //自定义异常 public class MyException extends Exception{ public MyException(String msg) { super(msg); }}
try-catch-finally-return的执行顺序
参考地址:http://blog.csdn.net/aaoxue/article/details/8535754
情况1:try块中没有抛出异常try和finally块中都有return语句
public static void main(String[] args) { int value = NoException(); System.out.println("最终值:"+value); } public static int NoException(){ int i=10; try{ System.out.println("i in try block is"+i); return --i; }catch(Exception e){ --i; System.out.println("i in catch - form try block is"+i); return --i; }finally{ System.out.println("i in finally - from try or catch block is"+i); return --i; } }
i in try block is10i in finally - from try or catch block is9最终值:8
执行顺序:执行try块,执行到return语句时,先执行return的语句,–i,但是不返回到main 方法,执行finally块,遇到finally块中的return语句,执行–i,并将值返回到main方法,这里就不会再回去返回try块中计算得到的值
情况2:try块中没有抛出异常,仅try中有return语句代码:
public static void main(String[] args) { int value = NoException(); System.out.println("最终值:"+value); } public static int NoException(){ int i=10; try{ System.out.println("i in try block is--"+i); return --i; }catch(Exception e){ --i; System.out.println("i in catch - form try block is--"+i); return --i; }finally{ System.out.println("i in finally - from try or catch block is--"+i); --i; System.out.println("i in finally block is--"+i); //return --i; } }
i in try block is--10i in finally - from try or catch block is--9i in finally block is--8最终值:9
try中执行完return的语句后,不返回,执行finally块,finally块执行结束后,返回到try块中,返回i在try块中最后的值
情况3:try块中抛出异常try,catch,finally中都有return语句
public static void main(String[] args) { int value = WithException(); System.out.println("最终值:"+value); } public static int WithException(){ int i=10; try{ System.out.println("i in try block is--"+i); i = i/0; return --i; }catch(Exception e){ System.out.println("i in catch - form try block is--"+i); --i; System.out.println("i in catch block is--"+i); return --i; }finally{ System.out.println("i in finally - from try or catch block is--"+i); --i; System.out.println("i in finally block is--"+i); return --i; } }
i in try block is--10i in catch - form try block is--10i in catch block is--9i in finally - from try or catch block is--8i in finally block is--7最终值:6
顺序,抛出异常后,执行catch块,在catch块的return的–i执行完后,并不直接返回而是执行finally,因finally中有return语句,所以,执行,返回结果6
情况4,catch中有return,finally中没有,同上,执行完finally语句后,依旧返回catch中的执行return语句后的值,而不是finally中修改的值
情况5:try和catch中都有异常,finally中无return语句
public static int CatchException(){ int i=10; try{ System.out.println("i in try block is--"+i); i=i/0; return --i; }catch(Exception e){ System.out.println("i in catch - form try block is--"+i); int j = i/0; return --i; }finally{ System.out.println("i in finally - from try or catch block is--"+i); --i; System.out.println("i in finally block is--"+i); //return --i; } }
i in try block is--10i in catch - form try block is--10i in finally - from try or catch block is--10i in finally block is--9Exception in thread "main" Java.lang.ArithmeticException: / by zero at exception.ExceptionTest0123.CatchException(ExceptionTest0123.java:29) at exception.ExceptionTest0123.main(ExceptionTest0123.java:17)
执行顺序:在try块中出现异常,到catch中,执行到异常,到finally中执行,finally执行结束后判断发现异常,抛出
try,catch中都出现异常,在finally中有返回
public static int CatchException(){ int i=10; try{ System.out.println("i in try block is--"+i); i=i/0; return --i; }catch(Exception e){ System.out.println("i in catch - form try block is--"+i); int j = i/0; return --i; }finally{ System.out.println("i in finally - from try or catch block is--"+i); --i; System.out.println("i in finally block is--"+i); return --i; } }
i in try block is--10i in catch - form try block is--10i in finally - from try or catch block is--10i in finally block is--9the method value is--8
执行顺序:try块中出现异常到catch,catch中出现异常到finally,finally中执行到return语句返回,不检查异常
- Java语法总结 - 异常
- Java异常处理总结
- Java异常处理总结
- Java异常总结
- Java异常处理总结
- Java异常处理总结
- java 异常处理总结
- Java异常处理总结
- java异常的总结
- Java 异常总结
- Java异常类总结
- JAVA异常题目总结
- Java异常 总结
- Java异常类总结
- Java异常处理总结
- java异常处理总结
- Java常见异常总结
- java异常总结(转)
- 投资网页设计的三个原因
- 有一字符串,包含n个字符。写一函数,将此字符串中从第m个字符开始的全部字符复制成为另一个字符串。
- [华为OJ--C++]037-挑7
- 关于spring mvc访问静态资源,包括html.js
- 人活着系列之芳姐和芳姐的猪
- java 异常总结
- iOS遇到问题小总结
- angular 路由,ui-router,ocLazyLoad的使用
- 二叉树的下一个结点
- 实习bug记录
- IE6,IE7,IE8,Firefox兼容的css hack问题
- 8月18号--Java基础查漏补缺
- Android layer type 小结
- android与H5互相调用