What's the configuration to notify the unhandl

2020-04-11 15:42发布

when in eclipse i use a method which throws an exception, it usually complains if it is not surrounded by a try/catch or if the exception is not thrown again. But for some exceptions (e.g. Integer.parseInt(string)) eclipse won't complain.

How do i set eclipse to complain for all not handled exceptions??

Thanks!

6条回答
在下西门庆
2楼-- · 2020-04-11 16:18

The simple answer is that you can't.

The longer answer is:

  • Checked versus unchecked exceptions is a fundamental part of the Java language.

  • It is not the role of the Eclipse compiler to give compilation errors or warnings for valid and perfectly acceptable Java.

  • You wouldn't want it to either, considering that the majority of statements could (in theory) throw or propagate exceptions such as NullPointerException, ArrayIndexOutOfBoundsException, OutOfMemoryError, and so on.

Yes, there are one or two "mistakes" ... such as NumberFormatException being an unchecked exception ... but a better way to deal with that would be (for example) to run PMD with some custom rules to pick up exceptions that "ought to be" treated as checked.

查看更多
爷、活的狠高调
3楼-- · 2020-04-11 16:24

It is not eclipse that is "set to complain" about exceptions.

See Checked and Unchecked Exceptions in Java.

查看更多
做自己的国王
4楼-- · 2020-04-11 16:28

RuntimeExceptions do not need to be declared and could be handled by any caller in the stack. Evaluating where that caller is would be impossible at compile-time.

查看更多
家丑人穷心不美
5楼-- · 2020-04-11 16:35

AFAIK, Eclipse uses the same checked exception and unchecked exceptions list that Java does. This is part of the definition of how Java works. I would be surprised if Eclipse allows you to override this.

In Java, all Throwable are checked except subclasses of RuntimeException and Error.

查看更多
三岁会撩人
6楼-- · 2020-04-11 16:37

This behaviour is defined by the java language definition. There are two types of Exceptions: Exceptions must be caught, RuntimeExceptions may occur but need no try/catch block.

查看更多
Deceive 欺骗
7楼-- · 2020-04-11 16:42

The compiler complains only if the checked exceptions are not handled. Here the Integer.parseInt(string) is expected to throw a numberformatexception which is unchecked or runtime exception.Since it is not complaing.

查看更多
登录 后发表回答