简体   繁体   English

为什么Integer.parseInt在没有try catch的情况下进行编译?

[英]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. 据我所知,在java中,抛出异常的函数不应该在没有try和catch或者在它上面的函数中减速的情况下编译。 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. 即使Integer.parseInt()抛出:NumberFormatException - 如果字符串不包含可解析的整数。

NumberFormatException extends RuntimeException which is an unchecked exception that does not need to be caught. NumberFormatException扩展RuntimeException ,这是一个不需要捕获的未经检查的异常。

在此输入图像描述

Excerpt from the Java Tutorial 摘自Java教程

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. 由于Java编程语言不需要捕获或指定未经检查的异常(RuntimeException,Error及其子类)的方法,因此程序员可能会编写仅抛出未经检查的异常或使其所有异常子类继承自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 NumberFormatException Api文档

From the Java language spec : Java语言规范

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. 换句话说,每个Throwable,即RuntimeException或子类以及每个Throwable,都是Error或子类。 They can be catched but catching or throws is not mandatory. 它们可以被捕获,但捕获或throws不是强制性的。

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. 也就是说,已检查的异常类都是Throwable的子类,而不是RuntimeException及其子类和Error及其子类。

In other words, every other Throwable . 换句话说,每一个Throwable They have to be thrown ( throws ) or catched. 他们必须被投掷( throws )或被抓住。

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扩展RuntimeException ,因此它是未经检查的异常类之一,并且不必被方法捕获或抛出。

NumberFormatException is a so-called unchecked exception, because it's a subtype of RuntimeException . NumberFormatException是一个所谓的未经检查的异常,因为它是RuntimeException的子类型。

In java, unchecked exceptions also compile without try-catch 在java中,未经检查的异常也可以在没有try-catch的情况下编译

NumberFormatException is a RuntimeException it is unchecked therefore doesn't need to be catched. NumberFormatException是一个RuntimeException它是未选中的,因此不需要捕获。

Please check the [documentation] if you don't know what a checked/unchecked Exception is 2 如果您不知道检查/未检查的Exception2,请检查[文档]

As far as I understand in java a function which throws an exception should not be compiled without a try and catch 据我所知,在java中,抛出异常的函数不应该在没有try和catch的情况下编译

Right, just change exception to Checked exception . 是的,只需将exception更改为Checked exception

Go through following to have a clear idea about these: 通过以下内容了解这些:

Effective Java Exceptions 有效的Java异常

Exceptions 例外

NumberFormatException is a RuntimeException. NumberFormatException是一个RuntimeException。 RuntimeExceptions are unchecked exceptions. RuntimeExceptions是未经检查的异常。 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://www.javapractices.com/topic/TopicAction.do?Id=129
http://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html http://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html

Exception in Java are of two kinds : checked exceptions and unchecked exceptions. Java中的异常有两种:已检查异常和未经检查的异常。

Checked exceptions are the one which need try-catch block or declaration. 已检查的异常是需要try-catch块或声明的异常。

Unchecked exception do not need that. 未经检查的异常不需要。 NumberFormatException is an unchecked exception. NumberFormatException是未经检查的异常。

Basicaly, unchecked Exception derive from RuntimeException , and thus does not need declaration or try-catch block. Basicaly,未经检查的异常派生自RuntimeException ,因此不需要声明或try-catch块。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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