Throw checked exceptions

2020-07-29 17:39发布

A few of my methods in Java throw exceptions such as NoSuchElementException, IllegalArgumentException, etc. But when using these methods, these exceptions appear to be unchecked. In other words, caller of my methods is not required to do a try/catch on my methods that throw those exceptions. I read around it seems that Exceptions by default are "checked" and only Errors are the ones "unchecked". But somehow, the exceptions I throw are also unchecked. It is weird.

How can I ensure that when my method throws an exception, the caller MUST catch the exception at compile time? Simply said, how can I throw a checked exception?

Thanks!

6条回答
放我归山
2楼-- · 2020-07-29 18:04

Exceptions that extends RuntimeException do not have to be declared.

You can declare the method: throws Exception or even throws Throwable and that will have to be handled (though it is not advised).

查看更多
Animai°情兽
3楼-- · 2020-07-29 18:10

See this hack, it might help (hope it's not OT).

public class Test {

    // No throws clause here
    public static void main(String[] args) {
        doThrow(new SQLException());
    }

    static void doThrow(Exception e) {
        Test.<RuntimeException> doThrow0(e);
    }

    @SuppressWarnings("unchecked")
    static <E extends Exception> void doThrow0(Exception e) throws E {
        throw (E) e;
    }
}
查看更多
等我变得足够好
4楼-- · 2020-07-29 18:12

Whether an exception is checked or not checked is NOT how you throw it or declare it, it is only dependent on whether the exception you choose is derived from RuntimeException or not. The ones you list above, all are derived from RuntimeException and so clients of your method do not need to catch them.

查看更多
Emotional °昔
5楼-- · 2020-07-29 18:16

Only RuntimeException and its subclasses are unchecked. (Well, Error and its subclasses are as well, but you shouldn't be messing with Errors.) All you need to do to throw a checked exception is ensure that it doesn't extend RuntimeException.

查看更多
Luminary・发光体
6楼-- · 2020-07-29 18:18

All sub-classes of Throwable are checked except sub-classes of Error and RuntimeException. (You can sub-class Throwable directly)

The compiler checks these Exception, however they have no special place at runtime. i.e. you can throw a checked Exception without the compiler knowing and it will behave normally.

e.g.

public static void throwChecked(Throwable t) /* no throws clause */ {
    Thread.currentThread().stop(t);
}

public static void main(String... args) /* no throws clause */ {
    throwChecked(new Throwable());
}

This compiles and prints the Throwable as you might expect.

查看更多
相关推荐>>
7楼-- · 2020-07-29 18:29

I think you should take a step back and learn the theory behind why to throw a checked exception vs an unchecked exception. The Java tutorial on exceptions is a good resource.

From the page entitled Unchecked Exceptions — The Controversy:

Runtime exceptions represent problems that are the result of a programming problem, and as such, the API client code cannot reasonably be expected to recover from them or to handle them in any way.

查看更多
登录 后发表回答