User defined Exceptions are the separate Exception classes defined by the user for specific purpose. A user defined exception can be created by simply sub-classing it to the Exception class. This allows custom exceptions to be generated (using throw) and caught in the same way as normal exceptions.

Exception Design Guidelines

The guidelines for defining  user defined Exception:

1.Exceptions are thrown only for exceptional conditions

Do not throw exceptions for expected outcomes. For example, a database lookup operation should not throw an exception if a lookup does not locate anything; it is normal for clients to occasionally look for things that are not there. It is harder for the caller to deal with exceptions than return values, because exceptions break the normal flow of control. Do not force the caller to handle an exception when a return value is sufficient.

2.Exceptions carry complete information

Ensure that exceptions carry all the data the caller requires to handle an error. If an exception carries insufficient information, the caller must make a second call to retrieve the missing information. However, if the first call fails, it is likely that subsequent calls will also fail.

3. Exceptions only carry useful information

Do not add exception members that are irrelevant to the caller.

4. Exceptions carry precise information

Do not lump multiple error conditions into a single exception type. Instead, use a different exception for each semantic error condition; otherwise, the caller cannot distinguish between different causes for an error.

Example:
class myCustomException extends Exception {
     // The class simply has to exist to be an exception
}
User defined exceptions can be created using the keyword using throw as follows:

                        throw new Throwable_subclass;
Examples
            throw new ArithmeticException ();
            throw new NumberFormatException ();

            class MyException extends Exception
            {
                        MyException (String message)
                        {
                                    super (message);
                        }
            }
            class TestMyException
{
            public static void main(String args[])
{
            int x=5,y=1000;
            try
            {
                        float z=(float)x/(float)y;
                        if(z<0.01)
                        {
                                    throw new MyException(“Number is too small”);
                        }
            }
            catch(MyException e)
            {
                        System.out.println(“Caught my exception”);
                        System.out.println(“e.getMessage());
            }
            finally
            {
                        System.out.println(“I am always here”);
            }
}

}
            Exception is a subclass of Throwable and therefore MyException is a subclass of Throwable class.An object of a class that extends Throwable can be thrown and caught.