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

📄 ex9.java

📁 java编程思想第四版习题答案
💻 JAVA
字号:
// exceptions/Ex9.java
// TIJ4 Chapter Exceptions, Exercise 9, page 460
/* Create three new types of exceptions. Write a class with a method that
* throws all three. In main(), call the method but only use a single catch
* clause that will catch all three types of exceptions.
*/
import static net.mindview.util.Print.*;

class ExceptionA extends Exception {
	ExceptionA(String msg) { super(msg); }
}

class ExceptionB extends Exception {
	ExceptionB(String msg) { super(msg); }
}

class ExceptionC extends Exception {
	ExceptionC(String msg) { super(msg); }
}

public class Ex9 {
	public static void f(int x) throws ExceptionA, ExceptionB, ExceptionC {
		if(x < 0) throw new ExceptionA("x < 0");
		if(x == 0) throw new ExceptionB("x == 0");
		if(x > 0) throw new ExceptionC("x > 0");
	}
	public static void main(String[] args) {
		try {
			f(0);
			f(1);
			f(-1);
		// will catch any Exception type:
		} catch(Exception e) {
			print("Caught Exception");
			e.printStackTrace(System.out);
		}
	}
}

⌨️ 快捷键说明

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