异常综合练习
下面示例经常出现在面试过程中,需要了解运行过程和执行结果
package com
.tedu
.exception
;
public class FinallyDemo4 {
public static void main(String
[] args
) {
System
.out
.println(test(null
));
System
.out
.println(test(""));
System
.out
.println(test("a"));
System
.out
.println(test(null
) + "," + test("") + "," +test("a"));
}
public static int test(String str
) {
try {
System
.out
.println("调用了test:" + str
);
return str
.charAt(0);
} catch (NullPointerException e
) {
System
.out
.println("出现了空指针!");
return 1;
} catch(StringIndexOutOfBoundsException e
) {
System
.out
.println("出现了字符越界!");
return 2;
} catch (Exception e
) {
System
.out
.println("未知错误!");
return 3;
} finally {
System
.out
.println("执行了finally");
}
}
}
转载请注明原文地址:https://blackberry.8miu.com/read-34422.html