What are user defined exceptions in JAVA?

1 Like

Java provides it’s in-built exceptions there are some situation where we need to create our own exceptions to handle particular error.
We must use following rule to create user defined exceptions:

1.User defined exception must be extend from particular Exception class.
2.The toString() method must be used in user defined exception.

Now used following code to understand user defined exceptions :


public class userdefinedException {

public static void main(String[] args) throws Exception{

    int a = getAge1();

    if (a < 0){
        throw new NegativeAgeException(a);
        System.out.println("Age get is " + a);

static int getAge1(){
    return -10;


Java supports exception handling by its try catch constructs. Exceptions are conditions which the JVM or applications developed in Java are unable to handle. The Java API defines exception classes for such conditions. These classes are derived from java.lang.Throwable class. User defined exceptions are those exceptions which an application provider or an API provider defines by extending java.lang.Throwable class.