Anshul Gupta
Anshul Gupta

Reputation: 277

"Get around" throws clause by subclassing Runtime Exception

Q: Is there any way to "get around" the strict restrictions placed on methods by the throws clause?

According to me we can "get around" throws clause by using try-catch but the answer I found is-

A: Yes. Suppose you have thought long and hard and have decided that you need to circumvent this restriction. This is almost never the case, because the right solution is to go back and redesign your methods to reflect the exceptions that you need to throw. Imagine, however, that for some reason a system class has you in a straitjacket. Your first solution is to subclass RuntimeException to make up a new, exempt exception of your own. Now you can throw it to your heart's content, because the throws clause that was annoying you does not need to include this new exception. If you need a lot of such exceptions, an elegant approach is to mix in some novel exception interfaces to your new Runtime classes. You're free to choose whatever subset of these new interfaces you want to catch (none of the normal Runtime exceptions need be caught), while any leftover (new) Runtime exceptions are (legally) allowed to go through that otherwise annoying standard method in the library.

I do not understand why do I need to subclass Runtime Exception when I can simply use try-catch? Also please give me the program where we subclass Runtime Exception to "get around" throws clause.

Upvotes: 0

Views: 269

Answers (1)

Josh Sullivan
Josh Sullivan

Reputation: 209

I think you may be thinking about the concept of "getting around" restrictions placed by the throws clause differently than the answer you are posting. Checked vs unchecked exceptions.

Unchecked exceptions do not need to be declared in a method's throws clause. Additionally, you may call methods that may throw unchecked exceptions with or without using a try/catch. For this reason, you can throw additional runtime exceptions from a given method without modifying its throws clause or the calling code.

A method that throws a checked (subclass of exception) exception, on the other hand, needs to declare these exceptions in its throws clause. Calling methods must wrap invocations of checked exception-throwing methods with try/catch, or add its own throws clause to its method signature.

public class TestClass {

    public static void main(String[] args) {
        new TestExceptionClass().uncheckedExceptionMethod();
        new TestExceptionClass().checkedExceptionMethod(); // does not compile, needs to be wrapped in try/catch or declared w/ throws keyword in main method
    }
}

class TestExceptionClass {

    void uncheckedExceptionMethod() {  // unchecked exceptions do not need to be included in throws
        throw new MyRuntimeException();
    }

    void checkedExceptionMethod() throws Exception {  // checked exceptions need to be included in throws
        throw new Exception();
    }
}

class MyRuntimeException extends RuntimeException { }

Upvotes: 0

Related Questions