检查和未检查异常;什么使他们不同? [英] Checked and unchecked exceptions; what makes them different?

查看:108
本文介绍了检查和未检查异常;什么使他们不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图得到例外的挂起,但遇到一些问题。我要解释一下我所知道的,并将粗体文本添加到我不确定的部分:

I've been trying to get the hang of exceptions but I've run into some problems. I'm going to explain what I know and add bold text to the parts I'm not sure of:

我知道有检查异常和未检查异常(后者是RuntimeException的所有子类?)。

I understand that there are Checked exceptions and Unchecked exceptions (The latter are all subclasses of RuntimeException?).

在可以抛出检查异常的方法中,方法签名需要指出这一点(至少如果我已经明白了我正在阅读关于异常的内容?)。

例如:

In methods where Checked exceptions can be thrown, the method signature NEEDS to state this (At least if I've understood what I've read about exceptions correctly?).
E.g.:

public void m() throws IOException{
...
}

对于未检查的异常,不需要执行此操作(不需要,但不能?)

This doesn't need to be done for Unchecked exceptions (It doesn't NEED to, but CAN it?)

我也知道您可以尝试一段代码,并且可能会抛出的任何异常将被捕获在catch部分(Try-Catch)中。 是否必须对已检查和未检查的异常进行此操作?何时需要这样做?

I also know that you can "try" a piece of your code and any exceptions that might be thrown there will be caught in the "catch" part (Try-Catch). Does this have to be done for both checked and unchecked exceptions? When do I have to do this?

为了澄清最后一个问题的第二部分,像

To clarify the second part of the last question, a class like

public class A
{
    private int[] items;

    public int first()
    {
        return items[0];
    }
}

将返回NullPointerException。但是没有Try-Catch部分,所以我什么时候需要使用Try-Catch,什么时候不要我?

Will return a NullPointerException. Yet there's no Try-Catch part, so when do I need to use Try-Catch and when don't I?

非常感谢!

Ps我已经在StackOverflow上阅读了关于这个主题的一些答案,但没有一个真正具体到我遇到的问题。

P.s. I've already read some answers regarding this topic on StackOverflow but none are really specific to the problems I'm experiencing.

推荐答案

了解异常最重要的一点是,您不需要抓住他们,至少不会直接发生在哪里。异常的此属性将其与返回值分开,并使其成为实现错误/异常事件处理通常需要的执行流模式的有用语言功能。

The most important thing to know about exceptions is that you don't need to catch them, at least not directly where they happen. This property of the exception sets it apart from the return value, and makes it a useful language feature to achieve execution flow patterns typically needed for error/exceptional event handling.

只有在Java中有关异常的分散注意事项才是被检查的异常,从而引起了处理它们的紧迫性。事实是,您不必处理检查的异常,但从异常引导到处理程序的完整调用路径必须包含 throws 声明。这从非常尴尬到不可能。标准的解决方法是立即捕获被检查的例外,只能抛出另一个未取消选中的异常,其中包含原始的

The only distracting thing about exceptions in Java are the checked exceptions, which induce a sort of urgency about handling them. Fact is, you don't have to handle a checked exception either, but the complete call path leading from the exception to the handler must be beset with throws declarations. This ranges from very awkward to impossible. The standard workaround for that is catching the checked exception right away, only to throw another, unchecked one, which wraps the original.

这篇关于检查和未检查异常;什么使他们不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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