用抓不带参数 [英] Using catch without arguments

查看:114
本文介绍了用抓不带参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之间有什么区别:

 
{
    MessageBox.Show(错误);
}

 赶上(异常前)
{
    MessageBox.Show(错误);
    //我们从不使用前,所以它更好地使用捕捉不带参数?
}


解决方案

由于.NET 2,如果你不调整配置​​?什么也没有。

在此之前,或一些配置好办法我不记得了precisely,也正在从非托管code抛出异常的可能性,其中的没有的被转换成一个例外兼容的对象。

请注意,有就是你指定类型,但是没有变量之间,另一种选择:

 赶上(例外)
{
   ...
}

个人而言,我会非常谨慎捕获异常,甚至没有的登录的吧。它可能如果你调用一个愚蠢的API是必需的,但它通常最好避免。

What is the difference between:

catch
{
    MessageBox.Show("Error.");
}

and:

catch (Exception ex)
{
    MessageBox.Show("Error.");
    //we never use ex, so is it better to use catch without arguments?
}

解决方案

As of .NET 2, if you don't tweak the configuration? Nothing.

Before then, or with some config tweak I can't remember precisely, there was the possibility of an exception being thrown from unmanaged code which didn't get converted into an Exception-compatible object.

Note that there's another option in between, where you specify the type but no variable:

catch (Exception)
{
   ...
}

Personally I'd be very wary of catching an exception without even logging it. It may be required if you're calling a boneheaded API, but it's generally best avoided.

这篇关于用抓不带参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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