的try / catch /抛出和try / catch(E)/扔E之间的差异 [英] The difference between try/catch/throw and try/catch(e)/throw e

查看:204
本文介绍了的try / catch /抛出和try / catch(E)/扔E之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是什么

try { }
catch
{ throw; }

try { }
catch(Exception e)
{ throw e;}

当我应该使用一个或其他?

And when should I use one or the other?

推荐答案

的结构

try { ... }
catch () { ... } /* You can even omit the () here */

try { ... }
catch (Exception e) { ... }

在类似,都将捕获的里面抛出每例外尝试块(并且,除非你只是以此来记录异常,应避免)。现在看看这些:

are similar in that both will catch every exception thrown inside the try block (and, unless you are simply using this to log the exceptions, should be avoided). Now look at these:

try { ... }
catch ()
{
    /* ... */
    throw;
}

try { ... }
catch (Exception e)
{
    /* ... */
    throw;
}

try { ... }
catch (Exception e)
{
    /* ... */
    throw e;
}

第一和第二的try-catch块是完全一样的东西,他们只是重新抛出当前异常,而且异常将保持其源和堆栈跟踪。

The first and second try-catch blocks are EXACTLY the same thing, they simply rethrow the current exception, and that exception will keep its "source" and the stack trace.

第三个try-catch块是不同的。当它抛出异常,它会改变源和堆栈跟踪,这样就会出现的异常已经从此方法抛出,从那时候行投è对包含该try-catch块的方法。

The third try-catch block is different. When it throws the exception, it will change the source and the stack trace, so that it will appear that the exception has been thrown from this method, from that very line throw e on the method containing that try-catch block.

哪一个?这真的取决于每个案件。

Which one should you use? It really depends on each case.

假设你有一个 .Save()方法的类,将持续到数据库中。比方说,你的应用程序的某个地方执行 Person.Save()方法。如果您的数据库拒绝拯救人,那么 .Save()将抛出一个异常。如果您使用在这种情况下抛出扔è?嗯,这取决于。

Let's say you have a Person class with a .Save() method that will persist it into a database. Let's say that your application executes the Person.Save() method somewhere. If your DB refuses to save the Person, then .Save() will throw an exception. Should you use throw or throw e in this case? Well, it depends.

我preFER做:

try {
    /* ... */
    person.Save();
}
catch(DBException e) {
    throw new InvalidPersonException(
       "The person has an invalid state and could not be saved!",
       e);
}

这应该把DBException作为新的例外是抛出的内部异常。所以,当你检查一下InvalidPersonException,堆栈跟踪将包含信息回保存方法(即可能足以为您解决问题),但你仍然可以访问原始异常,如果你需要它。

This should put the DBException as the "Inner Exception" of the newer exception being throw. So when you inspect this InvalidPersonException, the stack trace will contain info back to the Save method (that might be sufficient for you to solve the problem), but you still have access to the original exception if you need it.

作为最后的一句话,当你的期望的异常,你应该捕捉到一个特定的例外,不是一般例外,也就是说,如果你期待一个InvalidPersonException你应该preFER:

As a final remark, when you are expecting an exception, you should really catch that one specific exception, and not a general Exception, ie, if you are expecting an InvalidPersonException you should prefer:

try { ... }
catch (InvalidPersonException e) { ... }

try { ... }
catch (Exception e) { ... }

祝你好运!

这篇关于的try / catch /抛出和try / catch(E)/扔E之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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