異常選擇題

2018-07-03 15:11 更新

題目一

下面哪個不對

  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、程序中標(biāo)記的代碼的執(zhí)行順序? 2、改程序的最后返回值(外部調(diào)用時)?

程序按順序從上到下執(zhí)行到(2), 字符"t"轉(zhuǎn)換成整數(shù)失敗, 產(chǎn)生異常并被捕獲, 于是對a賦值成1, 并將此值作為此方法的返回值(可以這么認為, 該方法有一個存放返回值的空間, 此時將1放在此處). 由于存在finally塊, 在返回前將該方法的內(nèi)部變量a修改成2. 所以程序?qū)礃?biāo)記的順序執(zhí)行, 外部調(diào)用該方法時得到的結(jié)果是1

先執(zhí)行try或catch里里面的代碼,然后再執(zhí)行finally,再執(zhí)行try或catch里面的return.

題目三

寫出你最常見到的5個runtime exception

ClassCastException IllegalArgumentException NullPointerException IndexOutOfBoundsException ArrayIndexOutOfBoundsException+

題目四

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號