Home  >  Article  >  Java  >  Java Example - Custom Exception

Java Example - Custom Exception

黄舟
黄舟Original
2017-02-04 10:22:331470browse

The following example demonstrates the implementation of custom exceptions by inheriting Exception:

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

The output result of running the above code is:

Wrong input

The above is the Java example - the content of the custom exception , 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