What's the configuration to notify the unhandled exceptions in Eclipse?
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 ex开发者_如何转开发ceptions (e.g. Integer.parseInt(string)) eclipse won't complain.
How do i set eclipse to complain for all not handled exceptions??
Thanks!
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.
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.
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.
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
.
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.
It is not eclipse that is "set to complain" about exceptions.
See Checked and Unchecked Exceptions in Java.
精彩评论