异常综合练习
下面示例经常出现在面试过程中,需要了解运行过程和执行结果
package com.tedu.exception;
/**
* 异常综合练习
*
* @author Wildmess
*
*不管怎样都会执行的语句
*finally(关键字,常量),fianlly(异常中的块),finalize(Object类中的方法)
*/
public class FinallyDemo4 {
public static void main(String[] args) {
System.out.println(test(null));
System.out.println(test(""));
System.out.println(test("a"));//97
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");
}
}
}
335

被折叠的 条评论
为什么被折叠?



