exceptiondemo.java
来自「ssd3的教程 是我们老师给我们的 纯英文 有兴趣的可以」· Java 代码 · 共 73 行
JAVA
73 行
/**
* This class demonstrates exception throwing and catching.
*
* @author author name
* @version 1.0.0
*/
public class ExceptionDemo {
/**
* Calls methodA
*
* @param args not used
*/
public static void main(String[] args) {
methodA();
System.out.println("MethodA passed");
}
/**
* Calls methodB. If an exception occurs; catches it, reports the
* error, and terminate the program.
*/
public static void methodA() {
try {
methodB();
System.out.println("MethodB passed");
} catch (Exception e) {
e.printStackTrace();
// System.out.println(e.getMessage());
// System.out.println(e.toString());
System.exit(1);
}
}
/**
* Calls methodC. If an exception occurs, throws it to the calling
* method.
*
* @throws Exception when methodC is called.
*/
public static void methodB() throws Exception {
methodC();
System.out.println("MethodC passed");
}
/**
* Calls methodD. If an exception occurs, throws it to the calling
* method.
*
* @throws Exception when methodD is called.
*/
public static void methodC() throws Exception {
methodD();
System.out.println("MethodD passed");
}
/**
* Throws an exception
*
* @throws Exception whenever methodD is called.
*/
public static void methodD() throws Exception {
// System.out.println("This is an Exception Message");
throw new Exception("This is an Exception Message");
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?