以下實例示範了重載方法的異常處理:
/* author by w3cschool.cc Main.java */public class Main { double method(int i) throws Exception{ return i/0; } boolean method(boolean b) { return !b; } static double method(int x, double y) throws Exception { return x + y ; } static double method(double x, double y) { return x + y - 3; } public static void main(String[] args) { Main mn = new Main(); try{ System.out.println(method(10, 20.0)); System.out.println(method(10.0, 20)); System.out.println(method(10.0, 20.0)); System.out.println(mn.method(10)); } catch (Exception ex){ System.out.println("exception occoure: "+ ex); } } }
以上程式碼運行輸出結果為:
30.0 27.0 27.0 exception occoure: java.lang.ArithmeticException: / by zero
以上就是Java 實例- 重載方法異常處理的內容,更多相關內容請關注PHP中文網(www .php.cn)!