Home  >  Article  >  Java  >  Java Example - Overloaded Method Exception Handling

Java Example - Overloaded Method Exception Handling

黄舟
黄舟Original
2017-02-04 10:09:42900browse

The following example demonstrates the exception handling of overloaded methods:

/*
 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);
      }
   }
   }

The output result of running the above code is:

30.0
27.0
27.0
exception occoure: java.lang.ArithmeticException: / by zero

The above is the Java example - the content of overloaded method exception handling , for more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn