Eclipse does not detect missing try/catch anymore?

≡放荡痞女 提交于 2019-12-11 01:08:55

问题


In this code

String a = "notANumber";
Integer b = Integer.parseInt(a);

A try/catch is necessary since parseInt throws an NumberFormatException exception.
In my previous version of Eclipse, I used to got a warning telling that a try/catch was necessary but I can't figured out how to enable it on my current version of Eclipse which is

Eclipse Java EE IDE for Web Developers.
Version: Kepler Service Release 1

回答1:


A NumberFormatException is a RuntimeException.

You don't have to put try/catch for runtime exceptions.

From the javadoc of RuntimeException :

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine. RuntimeException and its subclasses are unchecked exceptions.

Unchecked exceptions do not need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.

And I seriously doubt any version of Eclipse required a try/catch for the code you show. You must be confused with another call (maybe a wrapper one declaring the exception, the fact the exception isn't checked doesn't mean you shouldn't, sometimes, explicitly catch it, in fact this one is probably most often explicitly catched).




回答2:


See the docs:

It's a Runtime Exception - It doesn't have to be checked, so the compiler won't check it.

See the JLS - 11.1.1. The Kinds of Exceptions for more valuable information:

The unchecked exception classes are the run-time exception classes and the error classes.



来源:https://stackoverflow.com/questions/21264613/eclipse-does-not-detect-missing-try-catch-anymore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!