在Java中抛出异常 [英] Throwing exceptions in Java

查看:132
本文介绍了在Java中抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于在Java中抛出异常的问题,这是我方面的一种误解,似乎我想为自己澄清。



我一直在阅读处理异常代码的两种基本方法是:



1)在try-block中抛出异常,使用throw new ...,而捕获它立即在一个catch块 - 所谓的try-throw-catch机制。



2。)在一个方法中抛出一个异常throw new ... 然后在方法的标题中声明此方法可能会抛出throws ...异常 - 所谓的pass-the-buck。



I最近看到,抛出异常,然后以同样的方法来捕捉它没有任何意义,这使我想到我是否以错误的方式理解这个事情,或者写过这个东西的人还有别的东西心里。处理异常的第一种方法不是这样(try-throw-catch机制)吗?我的意思是,它抛出一个异常,并以相同的方法捕获它。我已经看到,在一个方法中抛出异常是一个更好的做法,并以另一种方法来捕获它,但这只是一个(可能更好)的方式。另一方面也是合法正确的,不是吗?



请你给我一个评论?非常感谢你。

解决方案

当方法无法解决异常时,应该从方法抛出异常它自己的。



例如,一个 FileNotFoundException FileInputStream(new File( filename )),因为FileInputStream本身不能处理文件丢失的情况;该异常需要抛出,所以最终用户应用程序可以处理该问题。



有些情况下可以在方法中处理异常。例如,抛出 BadLocationException 的Document模型方法可以在一个足够智能的方法中处理。根据问题,异常可以被处理或重新抛出。



(无论如何,我会认为从try-catch块中抛出异常catch块可以执行代表非常糟糕的逻辑流程)


I have a question about throwing exceptions in Java, a kind of misunderstanding from my side, as it seems, which I would like to clarify for myself.

I have been reading that the two basic ways of handling exception code are:

1.) throwing an exception in a try-block with "throw new ...", and catching it immediately in a catch-block - the so called try-throw-catch mechanism.

2.) throwing an exception in a method with "throw new ..." and then declaring in the header of the method that this method might throw an exception with "throws ..." - the so called pass-the-buck.

I have recently read that "it doesn't make any sense to throw an exception and then catch it in the same method", which made me think whether I understand the thing in the wrong way, or the person who had written this had something else in mind. Doesn't the first way of handling exceptions does exactly this (the try-throw-catch mechanism) ? I mean, it throws an exception and catches it in the same method. I have read that it is a better practice to throw an exception in one method, and catch it in another method, but this is just one (probably better) way. The other way is also legal and correct, isn't it?

Would you, please, give me a comment on this ? Thank you very much.

解决方案

Exceptions should be thrown from a method when that method is incapable of resolving the exception on its own.

For example, a FileNotFoundException is thrown from new FileInputStream(new File(filename)) because the FileInputStream itself can't handle a case where a file is missing; that exception needs to get thrown so the end-user application can handle the problem.

There are some cases where exceptions could be handled within a method. For example, a Document model method throwing a BadLocationException could be handled within a sufficiently intelligent method. Depending on the problem, either the exception can be handled or re-thrown.

(Anyway, I'd argue that throwing an exception from within a try-catch block so the catch block can be executed represents really bad logic flow)

这篇关于在Java中抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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