📄 7.17mydefinedexceptionsample.java
字号:
public class MyDefinedExceptionSample {
public static void main (String args[]) {
System.out.println ("\n***Load a user defined exception***") ;
try {
//创建一个类ThrowExceptionClass 匿名对象,并调用其中的的方法
new ThrowExceptionClass ().throwDefaultException () ;
}
//捕获并处理异常
catch (MyDefinedException e) {
System.out.println ("Exception Information:"+ e.getMessage ()) ;
}
System.out.println( "\n***Load another user defined exception***" ) ;
try {
//创建一个类ThrowExceptionClass匿名对象,并调用其中的方法
new ThrowExceptionClass ().throwException ("A user exception is loaded!") ;
}
//捕获并处理异常
catch (MyDefinedException e) {
System.out.println ("Exception Information:"+e.getMessage ()) ;
}
}
}
class MyDefinedException extends Exception {
public MyDefinedException () {
super ("This is a user defined exception!") ; //调用父类的构造函数
}
public MyDefinedException (String message){
super (message) ; //调用父类的构造函数
}
}
class ThrowExceptionClass {
//抛出缺省参数的异常
public void throwDefaultException () throws MyDefinedException {
throw new MyDefinedException () ;
}
//抛出带有参数的异常
public void throwException(String message) throws MyDefinedException {
throw new MyDefinedException (message) ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -