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

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

问题描述

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() have

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中,您可能会知道,异常可以分为两种:一种需要抛出子句,或者如果不指定另一个,则必须处理。现在,请看下图:

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 子句。具体来说,这些两个类的任何子类都是错误 RuntimeException 。在您的情况下异常不是错误 RuntimeException 。因此,它是一个检查的异常,必须在 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.

从< a href =http://docs.oracle.com/javase/tutorial/essential/exceptions/definition.html> 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: 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 programm and reduce its clarity. The other reason is:


  • 运行时异常:通常由于程序员的错误而发生。例如,如果发生除以零的 ArithmeticException ,或者发生 ArrayIndexOutOfBoundsException ,那是因为我们不够小心在我们的编码。它们通常发生在我们的程序逻辑中的一些错误。因此,在我们的程序进入生产模式之前,必须先清除它们。在我们的程序发生时,我们的程序必须失败,所以我们的程序员可以在开发和测试时解决它。

  • Runtime Exceptions: They are usually happened due to programmers fault. 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:

  • Unchecked Exceptions — The Controversy
  • The Catch or Specify Requirement

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

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