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)