0 votes
139 views
in Java by
How to create custom unchecked exception in java? How to create custom unchecked exception for my project and how to use unchecked exception?

1 Answer

0 votes
by (4.4k points)
edited by

UnChecked Exceptions:- Exceptions that are not checked at the compile time that are called unchecked exceptions. Classes that extends RuntimeException comes under unchecked exceptions.

See the following code to create your custom UnChecked Exception in java:

package exceptions;

public class UncheckedException extends RuntimeException{

    public UncheckedException(String message) {
        this(message, null);
    }
    public UncheckedException(String message, Throwable th) {
        super(message,th);
    }
    public UncheckedException(Throwable th) {
        super(th);
    }
}

Use custom unchecked exception in our project:-

package exceptions;

public class TestException {

    public static void main(String[] args) {
           uncheckedExceptionTest();
    }

    private static void uncheckedExceptionTest() {
        throw new UncheckedException("Unchecked Exception");
    }

}

Output:-

Exception in thread "main" exceptions.UncheckedException: Unchecked Exception
    at exceptions.TestException.uncheckedExceptionTest(TestException.java:22)
    at exceptions.TestException.main(TestException.java:9)

Share:- Whatsapp Facebook Facebook


Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

...