codecamp

异常选择题

题目一

下面哪个不对

  1. RuntimeException is the superclass of those exceptions that must be thrown during the normal operation of the JVM

  1. A method is not required to declare in its throws clause any subclasses of RuntimeExeption that might be thrown during the execution of the method but not caught.

  1. An RuntimeException is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.

  1. NullPointerException is one kind of RuntimeException

答案是 3

RuntimeException an unchecked exception. It doesn't need to be explicitly declared or catched.

题目二

特殊情况就是里面加 return

举个例子去理解

public int getNumber() {


    int a = 0;


    try {
        String s = "t"; ------------------------(1)
        a = Integer.parseInt(s);-----------(2)
        return a;
    } catch (NumberFormatException e) {
        a = 1;-----------------------------------(3)
        return a;-------------------------------(4)
    } finally {
        a = 2;-----------------------------------(5)
    }
}

1、程序中标记的代码的执行顺序? 2、改程序的最后返回值(外部调用时)?

程序按顺序从上到下执行到(2), 字符"t"转换成整数失败, 产生异常并被捕获, 于是对a赋值成1, 并将此值作为此方法的返回值(可以这么认为, 该方法有一个存放返回值的空间, 此时将1放在此处). 由于存在finally块, 在返回前将该方法的内部变量a修改成2. 所以程序将按标记的顺序执行, 外部调用该方法时得到的结果是1

先执行try或catch里里面的代码,然后再执行finally,再执行try或catch里面的return.

题目三

写出你最常见到的5个runtime exception

ClassCastException IllegalArgumentException NullPointerException IndexOutOfBoundsException ArrayIndexOutOfBoundsException+

题目四

Checked 异常与 Runtime 异常
如果想要一个对象作为一个异常对象被抛出, 应该怎么做. (就是自己 DIY 一个异常啦)
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

Java 面试笔记

Java

多态 Polymorphism

Static 关键字

int 与 integer

Programme

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }