Runtime Exception continues from Exception and Custom Exception continues from Exception. Why does the latter eliminate compile time while the other does not?

In Java, I have an Exception class that extends from Exception

, but whenever I throw it the compiler says it needs to be caught / declared that the methods are throws

Exception.

When I use RuntimeException

which continues from Exception

, the compiler doesn't stop, it takes them as Execution Execution and we don't need to handle it.

Is there a way that MyException can be thrown from Exception and still have it as an exception at runtime? or What makes it as an opportunity in the classroomRuntimeException

private void compileTime() throws MyException{
        throw new MyException();
    }

    private void runTime() {
        throw new MyRuntimeException();
    }

    class MyException extends Exception {

    }
    class MyRuntimeException extends RuntimeException {

    }

      

+3


source to share


4 answers


RuntimeException is a subset of unchecked exceptions that can be recovered from.

thrown exceptions are not checked at compile time, which means the compiler does not require methods to catch or to specify (via throws) them.

The unmanaged exception classes are the RuntimeException class and its subclasses and the Error class and its subclasses. All other exception classes are marked with exception classes.



Please check the Exception Hierarchy through this image: exception hierarchy

In short, any exception that originates from "Exception" is a checked exception, whereas a class derived from a RuntimeException is not checked. RuntimeExceptions do not need to be handled explicitly by the caller.

+7


source


In no case. According to the specifications, exceptions extended from a class RuntimeException

or class Error

are considered unchecked exceptions (JLS 7, page 11.1.1).



+2


source


Throwing a compile-time exception is known as a Checked exception. As you say, the compiler will require you to include it in your method signature, and your callers will have to handle the possibility that it might be thrown.

RuntimeException is clearly intended for the case of "unchecked" exceptions.

From the docs

It is not required for a method to declare in its brochure any subclasses of RuntimeException that might be thrown during method execution but not detected.

So just extend the RuntimeException if you want an unchecked exception.

0


source


RuntimeException does not require try or catch compilation at the beginning of the method. Just because it happens at runtime, for example you want to pass the 14th element of a 10 element long array, or get a NullPointerException because you forgot to set the value of something, these cases cannot be predicted since the program will be fine work with the correct structures. Also as others say this is untested.

The exception is different, you have to tell the program what to do with the known exceptions. This means the complier will force you to handle the exception one way or another before it starts working.

0


source







All Articles