尝试/捕捉 +使用正确的语法
哪一个:
using (var myObject = new MyClass())
{
try
{
// something here...
}
catch(Exception ex)
{
// Handle exception
}
}
或
try
{
using (var myObject = new MyClass())
{
// something here...
}
}
catch(Exception ex)
{
// Handle exception
}
Which one:
using (var myObject = new MyClass())
{
try
{
// something here...
}
catch(Exception ex)
{
// Handle exception
}
}
OR
try
{
using (var myObject = new MyClass())
{
// something here...
}
}
catch(Exception ex)
{
// Handle exception
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我更喜欢第二个。也可以捕获与对象创建相关的错误。
I prefer the second one. May as well trap errors relating to the creation of the object as well.
由于 using 块只是 try/finally 的语法简化 (MSDN),我个人会选择以下内容,尽管我怀疑它与您的第二个选择有显着不同:
Since a using block is just a syntax simplification of a try/finally (MSDN), personally I'd go with the following, though I doubt it's significantly different than your second option:
这取决于。如果您使用的是 Windows Communication Foundation (WCF),则当
using
语句中的代理出现异常时,using(...) { try... }
将无法正常工作状态,即处置此代理将导致另一个异常。就我个人而言,我相信最小的处理方法,即仅处理您在执行时意识到的异常。换句话说,如果您知道
using
中变量的初始化可能会引发特定异常,我会用try-catch
包装它。同样,如果在using
主体内可能发生一些与using
中的变量没有直接关系的事情,那么我用另一个try
将其包装起来那个特殊的例外。我很少在catch
中使用Exception
。但我确实喜欢
IDisposable
和using
,所以我可能有偏见。It depends. If you are using Windows Communication Foundation (WCF),
using(...) { try... }
will not work correctly if the proxy inusing
statement is in exception state, i.e. Disposing this proxy will cause another exception.Personally, I believe in minimal handling approach, i.e. handle only exception you are aware of at the point of execution. In other word, if you know that the initialization of a variable in
using
may throw a particular exception, I wrap it withtry-catch
. Similarly, if withinusing
body something may happen, which is not directly related to the variable inusing
, then I wrap it with anothertry
for that particular exception. I rarely useException
in mycatch
es.But I do like
IDisposable
andusing
though so I maybe biased.如果您的 catch 语句需要访问 using 语句中声明的变量,那么 inside 是您唯一的选择。
如果您的 catch 语句需要在释放之前使用 using 中引用的对象,那么 inside 是您唯一的选择。
如果您的 catch 语句执行持续时间未知的操作(例如向用户显示消息),并且您希望在此之前处理掉您的资源,那么外部是您的最佳选择。
每当我遇到与此类似的场景时,try-catch 块通常位于调用堆栈中与 using 不同的方法中。像这样知道如何处理其中发生的异常的方法并不典型。
所以我的一般建议是在外面——远远的外面。
If your catch statement needs to access the variable declared in a using statement, then inside is your only option.
If your catch statement needs the object referenced in the using before it is disposed, then inside is your only option.
If your catch statement takes an action of unknown duration, like displaying a message to the user, and you would like to dispose of your resources before that happens, then outside is your best option.
Whenever I have a scenerio similar to this, the try-catch block is usually in a different method further up the call stack from the using. It is not typical for a method to know how to handle exceptions that occur within it like this.
So my general recomendation is outside—way outside.
两者都是有效的语法。这实际上取决于您想要做什么:如果您想捕获与创建/处置对象相关的错误,请使用第二个。如果没有,请使用第一个。
Both are valid syntax. It really comes down to what you want to do: if you want to catch errors relating to creating/disposing the object, use the second. If not, use the first.
我将在这里指出一件重要的事情:第一个不会捕获因调用
MyClass
构造函数而产生的任何异常。There is one important thing which I'll call out here: The first one will not catch any exception arising out of calling the
MyClass
constructor.从 C# 8.0 开始,您可以在某些情况下简化
using
语句以摆脱嵌套块,然后它仅适用于封闭块。所以你的两个例子可以简化为:
并且:
两者都非常清楚;然后,这将两者之间的选择减少到您想要对象的范围是什么、您想要在哪里处理实例化错误以及何时要处理它的问题。
From C# 8.0 on, you can simplify
using
statements under some conditions to get rid of the nested block, and then it just applies to the enclosing block.So your two examples can be reduced to:
And:
Both of which are pretty clear; and then that reduces the choice between the two to a matter of what you want the scope of the object to be, where you want to handle instantiation errors, and when you want to dispose of it.
如果您在 using() 块中初始化的对象可能会抛出任何异常,那么您应该采用第二种语法,否则两者同样有效。
在我的场景中,我必须打开一个文件,并且在Using()块中初始化的对象的构造函数中传递filePath,如果filePath错误/为空,它可能会抛出异常。因此在这种情况下,第二种语法是有意义的。
我的示例代码:-
If the object you are initializing in the Using() block might throw any exception then you should go for the second syntax otherwise both the equally valid.
In my scenario, I had to open a file and I was passing filePath in the constructor of the object which I was initializing in the Using() block and it might throw exception if the filePath is wrong/empty. So in this case, second syntax makes sense.
My sample code :-
从 C# 8.0 开始,我更喜欢使用第二个,就像这样
,然后
From C# 8.0, I prefer to use the second one same like this
and then