简单的try / catch不作任何用途的除外 [英] Simple try/catch not making any use of the exception

查看:126
本文介绍了简单的try / catch不作任何用途的除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经寻找的答案,我的问题,但没能找到一个。道歉,如果答案是有,我复制!

I have searched for an answer to my question but not been able to find one. Apologies if the answer is there and I am duplicating!

我一直看到的try / catch代码,如.....

I keep seeing try/catch code such as.....

try
{
    //Do whatever
}
catch (Exception ex)
{
    MessageBox.Show("Oops, something went wrong!");
}



这将导致一个警告前从未使用过。

Which will result in a warning ex is never used.

所以我的问题是......虽然前从未使用过有在申报任何利益?有人告诉我,也许它增加了详细的堆栈跟踪?有时候,我看到赶上(例外),该停止的警告,但这确实带来哪些好处,如果有的话?如果我是写这个,而不是使用异常以任何方式我不会宣布前...

So my question is... Although ex is never used is there any benefit in the declaration? I was told that maybe it adds detail to the stack trace? Sometimes I see catch(Exception) which stops the warning but what benefits does this bring, if any? If I was to write this and not use the exception in any way I wouldn't declare ex...

try
{
    //Do whatever
}
catch
{
    MessageBox.Show("Oops, something went wrong!");
}



不是一个很大的问题,但是这将是很好的了解肯定!

Not a big problem but it would be good to know for sure!

感谢

弗雷德

推荐答案

您可以使用下面的模式,仍然宣布具体的异常类型,没有一个变量,以确保结构化异常处理(SEH)仍然发生了:

You can use the following pattern, still declaring the specific exception type, without a variable, to ensure Structured Exception Handling (SEH) is still happening:

try
{
    //Do whatever
}
catch (IOException)
{
    MessageBox.Show("Oops, something went wrong in the IO!");
}
catch (Exception)
{
    MessageBox.Show("Oops, something went wrong!");
}

这是不是一个做法我通常会用,因为我可能会记录异常的详细信息,如果不重新抛出它。

This is not a practice I would normally use, as I would probably log the exception details if not rethrowing it.

这篇关于简单的try / catch不作任何用途的除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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