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
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With