Tutorials Point


  Java Basics Examples
  Java Tutorial
  Java Useful Resources
  Selected Reading

© 2011 TutorialsPoint.COM


  Home     References     Discussion Forums     About TP  

Java Examples - User defined Exception


previous next AddThis Social Bookmark Button


Problem Description:

How to create user defined Exception?

Solution:

This example shows how to create user defined exception by extending Exception Class.

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

Result:

The above code sample will produce the following result.

Wrong input


previous next Printer Friendly



  

Advertisement

Online Image Processing

Indian Baby Names