⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex1.java

📁 java编程思想第四版习题答案
💻 JAVA
字号:
// exceptions/Ex1.java
// TIJ4 Chapter Exceptions, Exercise 1, page 452
/* Create a class with a main(0 that throws an object of class Exception
* inside a try block. Give the constructor for Exception a String argument.
* Catch the exception inside a catch clause and print the String argument.
* Add a finally clause and print a message to prove you were there.
*/

class Exception1 extends Exception {
	public Exception1(String msg) {
		super(msg);
		System.out.println("Exception1(String msg)");
	}
}

public class Ex1 {
	public static void f() throws Exception1 {
		System.out.println("Throwing MyException from f()");
		throw new Exception1("From f()");
	}
	public static void main(String[] args) {
		try {
			f();
		} catch(Exception1 e) {
			System.err.println("Caught Exception1");
			e.printStackTrace();
		} finally {
			System.out.println("Made it to finally");
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -