这些尝试是否相当? [英] Are these try/catch'es equivalent?

查看:117
本文介绍了这些尝试是否相当?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方案

我有一种可以进行数据库操作的方法(就是说)。如果在该操作期间出现任何异常,我只想将该异常抛出给调用者。我不想在catch块中执行任何特定的任务,假设调用者将会执行与该异常有关的任何操作。在这种情况下,哪一个是适当的异常处理技术?

I have a method that does database operation (let's say). If during that operation any exception is raised, I just want to throw that exception to the caller. I don't want to do any specific task in the catch block, assuming caller will do whatever it wants to do with that exception. In this scenario, which one is appropriate exception handling technique?

try
{
    // Some work that may generate exception
}
catch(Exception)
{
    throw;
}
finally
{
    // Some final work
}

以上是否等同于以下try / catch / finally?

Is the above equivalent to the following try/catch/finally?

try
{
    // Some work that may generate exception
}
catch
{
    throw;
}
finally
{
    // Some final work
}

以上是否等同于以下try / finally?

Is the above equivalent to the following try/finally?

try
{
    // Some work that may generate exception
}
finally
{
    // Some final work
}

哪一个比其他更好?应该使用哪一个?

Which one is better than the other? Which one should be used?

推荐答案

不,它们不等同。

以下只会捕获从 System.Exception 继承的托管异常,然后执行 finally 块,无论是否抛出异常,都会发生。

The following will only catch managed exceptions that inherit from System.Exception and then executes the finally block, which will happen regardless of whether an exception was thrown or not.

try
{
   // Some work that may generate exception
}
catch (Exception)
{
   throw;
}
finally
{
   // Some final work
}



catch 没有指定异常类型的块



以下块也将捕获未被管理的 System.Exception 对象表示的非托管异常,然后执行 finally block,这将发生,无论是否抛出异常。

catch block without a specified exception type

The following catch block without a type specifier will also catch non-managed exceptions that are not necessarily represented by a managed System.Exception object, and then executes the finally block, which will happen regardless of whether an exception was thrown or not.

try
{
   // Some work that may generate exception
}
catch
{
   throw;
}
finally
{
   // Some final work
}



finally block without a catch block



如果你没有一个 catch block,你的终于将会执行,无论无论是否出现异常。

finally block without a catch block

If you do not have a catch block at all, your finally will still be executed regardless of whether or not an exception occoured.

try
{
   // Some work that may generate exception
}
finally
{
  // Some final work
}



他们什么时候相当?



如果您的 catch 块未指定例外,只包含 throw; 语句,最后两个确实是等价的。如果您不关心非托管异常,而您的 catch 块只包含 throw; 语句,所有

When are they equivalent?

In case your catch block doesn't specify an exception and only contains the throw; statement, the last two are indeed equivalent. In case you don't care about non-managed exceptions and your catch block only contains the throw; statement, all three can be considered equivalent.

以下两段代码包含一个微妙的区别。后者将重新抛出异常,这意味着它会重写异常的堆栈跟踪,所以这些肯定是等价的:

The following two pieces of code contain a subtle difference. The latter will re-throw the exception, meaning that it will rewrite the exception's stack trace, so these are definitely not equivalent:

catch (Exception e)
{
    throw;
}

catch (Exception e)
{
    throw e;
}



关于 使用



如果您使用最终 IDisposable ,以下两段代码几乎相当,但有一些微妙的区别:

A note about using

In case you use finally with an IDisposable, the following two pieces of code are almost equivalent, but with some subtle differences:


  • 当对象为null,使用语句不会给您一个 NullReferenceException

  • 当使用尝试 - finally 技术时,变量保留在范围内,虽然它是非常不要在处理完之后使用任何对象。但是,您仍然可以将变量重新分配给其他内容。

  • When the object is null, the using statement won't give you a NullReferenceException
  • When using the try-finally technique, the variable remains in scope, although it is very discouraged to use any object after it has been disposed. However you can still reassign the variable to something else.

某些obj = null;
try
{
obj = new Something()
//做某事
}
finally
{
obj.Dispose ();
}

Something obj = null; try { obj = new Something() // Do something } finally { obj.Dispose(); }

using (var obj = new Something())
{
    // Do something
}

这篇关于这些尝试是否相当?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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