Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • User-defined Custom Exception in Java.

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 271
    Comment on it

    User-defined Custom Exception in Java

    Inbuilt class Exception in java can be inherited to create our own exception.  

    Program creating user defined Exception by extending Exception class:

    // A Class that represents user-defined expception
    class DemoException extends Exception
    {
        public DemoException(String s)
        {
            // Call constructor of parent Exception
            super(s);
        }
    }
     
    // A Class that uses above MyException
    public class Main
    {
        public static void main(String args[])
        {
            try
            {
                // Throw an object of user defined exception
                throw new DemoException("User Defined Exception");
            }
            catch (DemoException ex)
            {
                System.out.println("Caught");
     
                // Print the message from DemoException object
                System.out.println(ex.getMessage());
            }
        }
    }

    Output:

    Caught
    User Defined Exception

    Explanation of above program:

    DemoException is user defined exception inheriting inbuilt Exception class. DemoException class Default constructor calls super class constructor. In main function within try a exception of DemoException type is throw using throw keyword, this exception is caught by catch clause thus executing statements inside catch clause. System.out.println(ex.getMessage()) prints the message passed by DemoException.  

 1 Comment(s)

  • This is a more elaborate custom exception: public class EntityHandlingException extends Exception {

    /**
     * Constructs an instance of <code>EntityHandlingException</code> with the
     * specified detail message.
     *
     * @param msg
     *            the detail message.
     */
    public EntityHandlingException(String msg) {
        super(msg);
    }
    
    /**
     * Constructs an instance of <code>EntityHandlingException</code> with the
     * specified cause.
     *
     * @param cause
     *            the cause.
     */
    public EntityHandlingException(Throwable cause) {
        super(cause);
    }
    
    /**
     * Constructs an instance of <code>EntityHandlingException</code> with the
     * specified detail message and cause.
     *
     * @param message
     *            the detail message.
     * @param cause
     *            the cause.
     */
    public EntityHandlingException(String message, Throwable cause) {
        super(message, cause);
    }
    

    }

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: