ex2(2).java
来自「Java编程宝典Thinking in Java第四版(4th)原书章节后习题及」· Java 代码 · 共 29 行
JAVA
29 行
// exceptions/Ex2.java
// TIJ4 Chapter Exceptions, Exercise 2, page 452
/* Define an object reference and initialize it to null. Try to call a method
* through this reference. Now wrap the code in a try-catch clause to catch the
* exception.
*/
public class Ex2 {
private static Integer i = null;
public static void main(String[] args) {
// leads to NullPointerException:
// System.out.println(i.toString());
try {
System.out.println(i.toString());
} catch(NullPointerException e) {
System.err.println("Caught NullPointerException");
e.printStackTrace();
}
try {
i = 10;
System.out.println(i.toString());
} catch(NullPointerException e) {
System.err.println("Caught NullPointerException");
e.printStackTrace();
} finally {
System.out.println("Got through it");
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?