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.

2
  • 1
    Integer.parseInt(null); also compiles. Commented Jan 10, 2013 at 10:44
  • But my sonarlint tells me to surround parseInt with a try &catch. Commented Dec 20, 2018 at 10:30

8 Answers 8

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

1

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

In java, unchecked exceptions also compile without try-catch

Comments

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

Comments

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

Comments

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

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.