What is a user defined exception?

What is a user defined exception?

At times, depending on the need of a program, a programmer might need to create his own set of exceptions. These exceptions are called as the User defined Exceptions.

The user defined exception class should extend from Exception class.
You may override the toString() method in order to display a user friendly message.

Example:
BloodGroupException.java
public class BloodGroupException extends Exception
{
   private String gp;
   public BloodGroupException (String gp)
   {
        this.gp = gp;
   }

public String toString()
{
   return "This is an incorrect Blood Group” ;
}
}
What are the different ways to generate an Exception?
Different ways to generate an Exception - There are 3 ways in which the exceptions are generated:...
How are try, catch and finally block organized?
How are try, catch and finally block organized? - The try block is the region of code where exceptions can get produced. So most of the code of execution lies in this region...
What is a throw in an Exception block?
What is a throw in an Exception block? - The different ways to use a throw are:...
Post your comment