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

📄 human30.java

📁 java编程思想第四版习题答案
💻 JAVA
字号:
// exceptions/Human30.java
// TIJ4 Chapter Exceptions, Exercise 30, page 500
/* Modify Human.java so that the exceptions inherit from
* RuntimeException. Modify main() so that the technique
* in TurnOffChecking.java is used to handle the different
* types of exceptions.
*/
import static net.mindview.util.Print.*;

class Annoyance extends RuntimeException {}
class Sneeze extends Annoyance {}


class WrapCheckedExceptions {
	void throwRuntimeException(int type) {
		try {
			switch(type) {
				case(0):
					throw new Annoyance();
				case(1):
					throw new Sneeze();
				case(2):
			throw new RuntimeException("Where am I?");
				default: return;
			}
		} 	catch(Exception e) {
			// Adapt to unchecked:
			throw new RuntimeException(e);
		}
	}
}

public class Human30 {
	public static void main(String[] args) {
		WrapCheckedExceptions wce =
			new WrapCheckedExceptions();
		for(int i = 0; i < 3; i++)
			try {
				if(i < 3)
					wce.throwRuntimeException(i);
				else
					throw new RuntimeException();
			}	catch(RuntimeException re) {
				try {
					throw re.getCause();
				} 	catch(Sneeze e) {
					print("Sneeze: " + e);
				}	catch(Annoyance e) {
					print("Annoyance: " + e);
				}	catch(Throwable e) {
					print("Throwable: " + e);
				}
			}
	}
}

⌨️ 快捷键说明

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