try / catch语句使用+正确的语法 [英] try/catch + using, right syntax
本文介绍了try / catch语句使用+正确的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
哪一个:
使用(VAR myObject的=新MyClass的())
{
尝试
{
//这里的东西...
}
赶上(异常前)
{
//处理异常
}
}
或
尝试
{
使用(VAR myObject的=新MyClass的())
{
//这里的东西...
}
}
赶上(异常前)
{
//处理异常
}
解决方案
我preFER第二个。不妨捕获错误与创建对象的为好。
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
}
解决方案
I prefer the second one. May as well trap errors relating to the creation of the object as well.
这篇关于try / catch语句使用+正确的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文