简体   繁体   中英

Why Integer.parseInt is compiling without try catch?

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.

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

在此输入图像描述

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

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.

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

In java, unchecked exceptions also compile without try-catch

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 is 2

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

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

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.

它是一个未经检查的异常。类RuntimeException及其子类是未经检查的异常类。这些异常可能出现在代码中的任何位置。因此,您可以捕获该异常并继续执行

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM