Java 實例 - 自定義異常
以下實例演示了通過繼承 Exception 來實現(xiàn)自定義異常:
/* author by w3cschool.cc TestInput.java */ class WrongInputException extends Exception { WrongInputException(String s) { super(s); } } class Input { void method() throws WrongInputException { throw new WrongInputException("Wrong input"); } } class TestInput { public static void main(String[] args){ try { new Input().method(); } catch(WrongInputException wie) { System.out.println(wie.getMessage()); } } }
以上代碼運行輸出結(jié)果為:
Wrong input
更多建議: