try/catch/throw 和 try/catch(e)/throw e 的区别 [英] The difference between try/catch/throw and try/catch(e)/throw e

查看:31
本文介绍了try/catch/throw 和 try/catch(e)/throw 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) { ... }

的相似之处在于两者都将捕获 try 块内抛出的每个异常(并且,除非您只是使用它来记录异常,否则应该避免).现在看看这些:

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 的方法上的那行 throw e-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 类,它将把它保存到数据库中.假设您的应用程序在某处执行 Person.Save() 方法.如果您的数据库拒绝保存 Person,则 .Save() 将抛出异常.在这种情况下,您应该使用 throw 还是 throw e ?嗯,这取决于.

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.

我更喜欢做的是:

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

这应该将 DBException 作为抛出的较新异常的内部异常".因此,当您检查此 InvalidPersonException 时,堆栈跟踪将包含返回给 Save 方法的信息(这可能足以让您解决问题),但如果需要,您仍然可以访问原始异常.

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.

最后要说的是,当您期望一个异常时,您应该真正捕获那个特定的异常,而不是一般的Exception,即,如果您期望您应该更喜欢 InvalidPersonException:

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/throw 和 try/catch(e)/throw e 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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