Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse does not detect missing try/catch anymore?

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
like image 500
Max Avatar asked Dec 09 '22 10:12

Max


2 Answers

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).

like image 95
Denys Séguret Avatar answered Jan 05 '23 13:01

Denys Séguret


See the docs:

enter image description here

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.

like image 20
Maroun Avatar answered Jan 05 '23 13:01

Maroun