📄 mycheckedexception.java
字号:
import java.io.*;
//自己的例外类
class MyCheckedException extends Exception
{
public MyCheckedException()
{
super();
}
public MyCheckedException(String s)
{
super(s);
}
}
class Catapult
{
//方法入口
public static void main(String args[])
{
System.out.println("Starting Catapult");
myMethod1();
}
public static void myMethod1()
{
try
{
System.out.println("Entering myMethod1()");
//方法myMethod2()抛出了例外,所以必须被捕捉
myMethod2();
System.out.println("This line never executes");
}
catch(MyCheckedException e)
{
//打印个人例外信息
System.out.println("Caught MyCheckedException in myMethod1");
System.out.println("Printing stack trace of the Exception's origin:");
//打印系统例外栈
e.printStackTrace();
}
finally
{
//调用finally方法
System.out.println("Executing myMethod1() finally clause");
}
}
//方法通过throws关键字抛出了自定义的例外
public static void myMethod2() throws MyCheckedException
{
try
{
System.out.println("Entered MyMethod2 and throwing MyCheckedException");
//使用throw关键字强制抛出例外
throw new MyCheckedException("Jim's Exception");
}
finally
{
System.out.println("Executing myMethod2() finally clause");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -