JAVA Exception异常总结(JAVA Exception面试笔试总结)
来源:互联网 发布:釜山行 知乎 编辑:程序博客网 时间:2024/06/05 07:51
这方面主要有以下几种类型的题目:
1 try中throw一个exception,能否直接catch?
public void method1(){try{throw new StringIndexOutOfBoundsException("method1 catch");}catch(Exception e){System.out.println(e.getMessage());}}
解答:这个如果catch里的exception是try中throw的异常或者它的父类,都是可以catch到的。
2.有多个catch的情况,如下,第二个catch是否会catch到?
/** * 有多个catch的情况 */public void method2(){try{throw new StringIndexOutOfBoundsException("method2");}catch(StringIndexOutOfBoundsException e){System.out.println("method2 catch 1");}catch(Exception e){System.out.println("method2 catch 2");}}解答:catch不管有多少个,也不管catch中的exception是否是父类,只要有一个catch到了,其他的就不会再catch到的
3. catch中throw new exception,下边继续catch能否catch到?
/** * catch中throw new exception,下边继续catch能否catch到? */public void method3(){try{throw new StringIndexOutOfBoundsException("method3 1");}catch(StringIndexOutOfBoundsException e){System.out.println(e.getMessage());throw new StringIndexOutOfBoundsException("method3 2");}catch(Exception e){System.out.println(e.getMessage());}}解答:如果这个catch执行的话,抛出了新的异常,就直接报异常导致程序暂停了。下面一个catch不能继续catch到刚才抛出的异常
4.catch中和finally中都throw new exception?
/** * catch中和finally中都throw new exception */@SuppressWarnings("finally")public void method4(){try{throw new StringIndexOutOfBoundsException("method4 1");}catch(StringIndexOutOfBoundsException e){System.out.println(e.getMessage());throw new StringIndexOutOfBoundsException("method4 2");}catch(Exception e){System.out.println(e.getMessage());}finally{throw new StringIndexOutOfBoundsException("method4 finally");}}解答:最后生效的是finally里的exception.
5.try中执行哪些代码?catch后代码是否执行?
/** * try中代码执行哪些?try catch 后面代码是否执行?输出什么 */public void method5(){try{System.out.println("method step1");new String("abc").subSequence(0, 5);System.out.println("method step2");}catch(StringIndexOutOfBoundsException e){System.out.println(e.getMessage());}catch(Exception e){System.out.println(e.getMessage());}System.out.println("method step3");}解答:这个里里面,try中抛出异常后,try里下面的代码就不执行了,直接进入了catch,如果try中抛出的异常被catch到了,那么catch后面的代码还是继续执行的。
6.异常是否必须catch或者方法头上throw
看这道题,A 说33行必须放在try中,33行调用method1,此方法中已经catch了testexception,但是又throw了新的RuntimeException,实际上RuntimeException是不用显示捕获的,所以A 错误,B正确
C说方法头上必须声明throw ,也是错误的
D说调用method2时候不用try,这个是不对的,因为throw的TestException不是RuntimeException,因此必须捕获。
- JAVA Exception异常总结(JAVA Exception面试笔试总结)
- java Exception j2ee异常总结
- java中的(Exception)异常总结
- JAVA常见异常总结(runtime exception)
- Java基础学习总结---------异常Exception(2)
- Exception-异常处理(java)的总结
- Java基础之异常Exception---总结
- 总结java的exception
- Java Exception总结
- java中Exception总结
- Java的异常Exception----(最简单明了的总结)
- Java-Exception(异常)
- Java checked Exception Nonchecked exception 总结
- Java基础<异常机制>总结(异常体系、Exception特点、异常处理方式)
- java 异常知识点总结 error exception throw throws
- java中的异常(Exception)
- Java异常Exception(一)
- Java异常Exception(二)
- Oracle中的rownum和rowid
- dbms_job与dbms_ijob的对比
- Bug Report For .Net (zz.IS2120@BG57IV3)
- I2C器件的从设备地址设置
- android 从asserts 读取图片做背景
- JAVA Exception异常总结(JAVA Exception面试笔试总结)
- ASP.NET中cookie读写方法
- getopt与getopt_long
- 关于Application.DoEvents()
- 找出口程序
- c# 将日志文件显示在RichTextBox控件里
- 数据 表之间的连接分为三种:
- sql server中datetime格式转换
- IE6中经常遇到的两个兼容性问题