Why Integer.parseInt is compiling without try catch?
Asked Answered
N

8

5

As far as I understand in java a function which throws an exception should not be compiled without a try and catch or a deceleration in the function above it. How come then this code is legitimate and dont crush?

public static void main(String[] args) {
    Integer.parseInt("33");
}

even though Integer.parseInt() Throws: NumberFormatException - if the string does not contain a parsable integer.

Ninos answered 10/1, 2013 at 10:36 Comment(2)
Integer.parseInt(null); also compiles.Cyanosis
But my sonarlint tells me to surround parseInt with a try &catch.Masson
A
14

NumberFormatException extends RuntimeException which is an unchecked exception that does not need to be caught.

enter image description here

Excerpt from the Java Tutorial

Because the Java programming language does not require methods to catch or to specify unchecked exceptions (RuntimeException, Error, and their subclasses), programmers may be tempted to write code that throws only unchecked exceptions or to make all their exception subclasses inherit from RuntimeException. Both of these shortcuts allow programmers to write code without bothering with compiler errors and without bothering to specify or to catch any exceptions. Full Article

NumberFormatException Api Docs

Antwanantwerp answered 10/1, 2013 at 10:38 Comment(0)
M
2

From the Java language spec:

The unchecked exception classes are the runtime exception classes and the error classes.

In other words, every Throwable, that is a RuntimeException or a subclass and every Throwable, that is an Error or a subclass. They can be catched but catching or throws is not mandatory.

The checked exception classes are all exception classes other than the unchecked exception classes. That is, the checked exception classes are all subclasses of Throwable other than RuntimeException and its subclasses and Error and its subclasses.

In other words, every other Throwable. They have to be thrown (throws) or catched.

NumberFormatException extends RuntimeException and therefore it is one of the unchecked exception classes and doesn't have to be catched or thrown be the method.

Maharashtra answered 10/1, 2013 at 10:48 Comment(0)
Y
1

NumberFormatException is a so-called unchecked exception, because it's a subtype of RuntimeException.

In java, unchecked exceptions also compile without try-catch

Yesteryear answered 10/1, 2013 at 10:38 Comment(0)
R
0

NumberFormatException is a RuntimeException it is unchecked therefore doesn't need to be catched.

Please check the [documentation] if you don't know what a checked/unchecked Exception is2

Radioluminescence answered 10/1, 2013 at 10:39 Comment(0)
C
0

As far as I understand in java a function which throws an exception should not be compiled without a try and catch

Right, just change exception to Checked exception.

Go through following to have a clear idea about these:

Effective Java Exceptions

Exceptions

Calia answered 10/1, 2013 at 10:39 Comment(0)
A
0

NumberFormatException is a RuntimeException. RuntimeExceptions are unchecked exceptions. What you say is mandatory for checked exception, but not required for unchecked exception.

Couple of link:
http://www.javapractices.com/topic/TopicAction.do?Id=129
http://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html

Ascendant answered 10/1, 2013 at 10:39 Comment(0)
U
0

Exception in Java are of two kinds : checked exceptions and unchecked exceptions.

Checked exceptions are the one which need try-catch block or declaration.

Unchecked exception do not need that. NumberFormatException is an unchecked exception.

Basicaly, unchecked Exception derive from RuntimeException, and thus does not need declaration or try-catch block.

Unguarded answered 10/1, 2013 at 10:39 Comment(0)
C
0

It is an unchecked exception.The class RuntimeException and its subclasses are unchecked exceptions classes.These exceptions can occur anywhere in your code.So it is upto you to catch that exception and proceed with execution

Chickasaw answered 10/1, 2013 at 10:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.