?? 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 + -