为什么是“抛出异常"?调用函数时需要吗? [英] Why is "throws Exception" necessary when calling a function?

查看:31
本文介绍了为什么是“抛出异常"?调用函数时需要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class throwseg1
{
    void show() throws Exception
    {
        throw new Exception("my.own.Exception");
    }

    void show2() throws Exception  // Why throws is necessary here ?
    {
        show();
    }

    void show3() throws Exception  // Why throws is necessary here ?
    {
        show2();
    }

    public static void main(String s[]) throws Exception  // Why throws is necessary here ?
    {
        throwseg1 o1 = new throwseg1();
        o1.show3();
    }
}

为什么编译器报告方法 show2()show3()main()

Why compiler reports that methods show2(), show3(), and main() have

未报告的异常 必须捕获或声明要抛出的异常

unreported exception Exception that must be caught or declared to be thrown

当我从这些方法中删除 throws Exception 时?

when I remove throws Exception from these methods?

推荐答案

在 Java 中,您可能知道,异常可以分为两种:一种需要 throws 子句,或者必须在以下情况下处理你没有指定一个,另一个没有.现在,请看下图:

In Java, as you may know, exceptions can be categorized into two: One that needs the throws clause or must be handled if you don't specify one and another one that doesn't. Now, see the following figure:

在 Java 中,您可以抛出任何扩展 Throwable 类的东西.但是,您不需要为所有类指定 throws 子句.具体而言,属于 ErrorRuntimeException 或这两者的任何子类的类.在您的情况下,Exception 不是 ErrorRuntimeException 的子类.因此,它是一个已检查的异常并且必须在 throws 子句中指定,如果您不处理该特定异常.这就是您需要 throws 子句的原因.

In Java, you can throw anything that extends the Throwable class. However, you don't need to specify a throws clause for all classes. Specifically, classes that are either an Error or RuntimeException or any of the subclasses of these two. In your case Exception is not a subclass of an Error or RuntimeException. So, it is a checked exception and must be specified in the throws clause, if you don't handle that particular exception. That is why you needed the throws clause.

来自 Java 教程:

异常是在程序执行过程中发生的一个事件,它破坏了程序指令的正常流程.

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.

现在,如您所知,异常分为两种:已检查和未检查.为什么要进行这些分类?

Now, as you know exceptions are classified into two: checked and unchecked. Why these classification?

Checked Exception:它们用于表示可以在程序执行过程中恢复的问题.它们通常不是程序员的错.例如,用户指定的文件不可读,或者没有可用的网络连接等,在所有这些情况下,我们的程序不需要退出,而是可以采取诸如提醒用户或进入回退等操作机制(如网络不可用时离线工作)等

Checked Exception: They are used to represent problems that can be recovered during the execution of the program. They usually are not the programmer's fault. For example, a file specified by user is not readable, or no network connection available, etc., In all these cases, our program doesn't need to exit, instead it can take actions like alerting the user, or go into a fallback mechanism(like offline working when network not available), etc.

未经检查的异常:它们又可以分为两种:错误和运行时异常.取消检查它们的原因之一是它们数量众多,并且需要处理所有这些会使我们的程序变得混乱并降低其清晰度.另一个原因是:

Unchecked Exceptions: They again can be divided into two: Errors and RuntimeExceptions. One reason for them to be unchecked is that they are numerous in number, and required to handle all of them will clutter our program and reduce its clarity. The other reason is:

  • 运行时异常:它们通常是由于程序员的错误而发生的.例如,如果发生被零除的ArithmeticException 或发生ArrayIndexOutOfBoundsException,那是因为我们的编码不够仔细.它们的发生通常是因为我们的程序逻辑中存在一些错误.因此,必须在我们的程序进入生产模式之前清除它们.从某种意义上说,它们是未经检查的,我们的程序必须在它发生时失败,以便我们程序员可以在开发和测试时自行解决.

  • Runtime Exceptions: They usually happen due to a fault by the programmer. For example, if an ArithmeticException of division by zero occurs or an ArrayIndexOutOfBoundsException occurs, it is because we are not careful enough in our coding. They happen usually because some errors in our program logic. So, they must be cleared before our program enters into production mode. They are unchecked in the sense that, our program must fail when it occurs, so that we programmers can resolve it at the time of development and testing itself.

错误:错误是程序通常无法恢复的情况.例如,如果发生StackOverflowError,我们的程序就不能做很多事情,例如增加程序函数调用堆栈的大小.或者,如果发生 OutOfMemoryError,我们就无法增加程序可用的 RAM 量.在这种情况下,最好退出程序.这就是为什么它们没有被选中的原因.

Errors: Errors are situations from which usually the program cannot recover. For example, if a StackOverflowError occurs, our program cannot do much, such as increase the size of program's function calling stack. Or if an OutOfMemoryError occurs, we cannot do much to increase the amount of RAM available to our program. In such cases, it is better to exit the program. That is why they are made unchecked.

有关详细信息,请参阅:

For detailed information see:

这篇关于为什么是“抛出异常"?调用函数时需要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆