User Defined Exception In Java

User defined exception are the exceptions defined by the programmer.

User Defined Exception In Java

User defined exception are the exceptions defined by the programmer.

User Defined Exception In Java

User Defined Exception In Java

In terms of programming when we talk about an exception, the one thing strikes into our mind that something is happening wrong during execution of program. Exception, an event that may generate within a method and caused to disrupts the normal flow of statements of program. In Java, when an exception occurs an exception object is created by the method (where exception has occurred) and the information of error, its type, and state of the program is kept into it and hands it off to the runtime system, called "Throwing an exception". The developer of Java has provided various corresponding Exceptions, called Built-in exceptions that may be thrown in different situations. But, it may happened that when you are developing an application and you faces the condition in which your application may prone to generate an error and throw an exception rather than the built-in exceptions. Then you will be required to create your own exception. A user defined exception can also be created for customizing the error message that is to be thrown.

To write a code within a method that may prone to generate an error in a condition then you should write your code using following java keywords :

  • try
  • catch
  • finally
  • throw
  • throws

You may use the above keywords when your line of code or method is prone to threw an exception. java.lang.Throwable is the super class of all the exceptions and errors in Java and the java.lang.Exception is the super class of all the exception classes. It allows for writing the code that indicates the condition of your application might want to caught. So for defining a user defined exception your exception class should be the subclass of java.lang.Exception class.

java.lang.Throwable class has some of the methods that are inherited by the Exception class and its subclass. You may also used it for customize your exception. These are as follows :

  • String toString( ) : Gives you a String object and description of the exception. This  method is called by the println ( ) method when an object of Throwable is passed to it as argument.
     
  • String getMessage( ) : Gives you the description of the exception in program
     
  • Throwable fillInStackTrace( ) :  Gives you aThrowable Object that contains a stack trace.
     
  • void print StackTrace( ) :  Gives you and print the stack trace.
     
  • void printStackTrace(PrintStream stream) :  Return the stack trace to a specific defined stream
     
  • String getLocalizedMessage() : Return the Localized description of the exception

Example

Here I am giving a simple example which will demonstrate how can you create a user defined exception in Java. The user defined exception describes the specific messages to be displayed when an exception is generated. To create a user defined exception at first we will create a class which will extend the java.lang.Exception class that we can specify the message format, condition for throwing an exception and many more things. Then we will create an another class where will write a code that may be prone to throw an exception. We will write the exception thrown code using try, catch, and finally.

MyException.java

class MyException extends Exception {
        private int detail;

        MyException(int a) {
                detail = a;
        }

        public String toString() {
                return "MyException[" + detail + "}";

        }
}

UserDefinedException.java

class UserDefinedException {
static void compute(int num) throws MyException {
System.out.println("Called computer(" + num + "]");
if (num < 10)
{ 
System.out.println("Number Is Less Than 10");
}
else
throw new MyException(num);
}

public static void main(String args[]) {
try {
compute(9);
compute(10);

} catch (MyException me) {
System.out.println("Caught " + me);
}
}
}

Output :

When you will compile and execute the UserDefinedException.java file then you will get the output as follows :

Download Source Code