catch(Exception ex)Vs catch(Exception) [英] catch(Exception ex) Vs catch (Exception)

查看:110
本文介绍了catch(Exception ex)Vs catch(Exception)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对你们来说很好很简单...


是否有时间使用Catch(Exception)而不是创建

的实例例外,如Catch(Exception ex)?

Nice and simple one for you all...

Is there a time to use Catch(Exception) rather than creating an instance of
the Exception, as in Catch(Exception ex)?

推荐答案

很好,很简单的一个...
Nice and simple one for you all...

>

是否有时间使用Catch(Exception)而不是创建Exception的

实例,如Catch(Exception ex) )?
>
Is there a time to use Catch(Exception) rather than creating an
instance of the Exception, as in Catch(Exception ex)?



如果你不想使用实例,你不需要使用catch(Exception ex)

。 IOW,如果您只是想知道发生了特定的异常

但不需要任何细节,可以省略ex。


例如:


string text = null;

try

{

Console.WriteLine (text.ToString());

}

catch(NullReferenceException)

{

Console.WriteLine( text is NULL!;

}

最好的问候,

Dustin Campbell

Developer Express公司

You don''t need to use catch(Exception ex) if you don''t want to use an instance
of it. IOW, if you just want to know that a specific exception has occurred
but don''t need any of the details, you can omit the ex.

For example:

string text = null;
try
{
Console.WriteLine(text.ToString());
}
catch (NullReferenceException)
{
Console.WriteLine("text is NULL!");
}
Best Regards,
Dustin Campbell
Developer Express Inc.


这取决于你是否想要做任何事情,只要你已经抓住它,或者如果你想重新获得它 - 抛出它。

在类似的主题上,这篇文章解释了

" throw"和抛出前:

http://mattgollob.blogspot.com/2006/...ifference.html

cashdeskmac写道:
It depends if you want to do anything with the exception once you''ve
caught it, or if you want to re-throw it.
On a similar subject, this article explains the difference between
"throw" and "throw ex":

http://mattgollob.blogspot.com/2006/...ifference.html

cashdeskmac wrote:

很好,简单的一个适合你...


是否有时间使用Catch(Exception)而不是创建一个实例

Exception,如Catch(Exception ex)?
Nice and simple one for you all...

Is there a time to use Catch(Exception) rather than creating an instance of
the Exception, as in Catch(Exception ex)?





cashdeskmac写道:
Hi,

cashdeskmac wrote:

很好,很简单,适合所有人...


是否有时间使用Catch(Exception)而不是创建

的实例异常,如在Catch(Exception ex)中?
Nice and simple one for you all...

Is there a time to use Catch(Exception) rather than creating an instance of
the Exception, as in Catch(Exception ex)?



这两种情况都不会创建Exception实例。该实例存在,并且之前通过调用新的ArgumentException(...)创建了


仅new关键字确实创建了对象。


您显示的代码的唯一区别在于,在一种情况下,您在Exception实例上获得了一个

引用(ex ),而在另一种情况下你

不要。如果你有一个实例,并且不使用它,你会在编译时得到一个警告

。这就是为什么你有时候更喜欢使用catch(

Exception),而没有ex。


这种情况​​可能会发生,例如,当你想要做一些清理时

发生错误:


StreamWriter swr = null;


试试

{

//做点什么

}

catch(例外)

{

throw;

}

终于

{

//这如果有错误则执行代码

if(swr!= null)

swr.Close();

}


在上面的例子中,我没有做任何异常,因此我使用

(例外)而没有ex。如果我想做一些日志记录,或者在另一个自己的日志中包含

异常,我会这样做:


catch(Exception ex)

{

Trace.WriteLine(ex.Message);

MyOwnException myEx = new MyOwnException(" Error",ex);

抛出myEx;

}


在这种情况下,我需要ex的引用,所以我必须声明它。


另外,请注意,如果你像第一个例子那样重新抛出异常,

你应该使用throw;而不是抛出前;见
http://geekswithblogs.net /lbugnion/a.../29/92708.aspx


HTH

问候,

Laurent

-

Laurent Bugnion,GalaSoft

软件工程: http://www.galasoft-LB.ch

PhotoAlbum: http://www.galasoft-LB.ch/pictures

支持加尔各答的孩子: http://www.calcutta-espoir.ch

Both cases do not create an Exception instance. The instance exists, and
was created earlier by calling new ArgumentException( ... ) for example.
Only the "new" keyword really creates objects.

The only difference in the code you show is that, in one case, you get a
reference on the Exception instance (ex), and in the other case you
don''t. If you have an instance, and don''t use it, you will get a warning
when you compile. That''s why you sometimes prefer to use catch (
Exception ), without the "ex".

Such case can occur, for example, when you want to do some clean up when
an error happen:

StreamWriter swr = null;

try
{
// do something
}
catch ( Exception )
{
throw;
}
finally
{
// This code is executed if there is an error or not
if ( swr != null )
swr.Close();
}

In the example above, I don''t do anything with the exception, thus I use
( Exception ) without ex. If I wanted to do some logging, or maybe wrap
the Exception in another own one, I would do:

catch ( Exception ex )
{
Trace.WriteLine( ex.Message );
MyOwnException myEx = new MyOwnException( "Error", ex );
throw myEx;
}

In that case, I''d need the reference to ex, so I must declare it.

Also, note that if you re-throw the exception like in the first example,
you should use "throw;" and not "throw ex;". See
http://geekswithblogs.net/lbugnion/a.../29/92708.aspx

HTH
Greetings,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch


这篇关于catch(Exception ex)Vs catch(Exception)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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