.Net例外 [英] .Net Exceptions

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

问题描述

有没有办法找出特定的.net函数是否会在没有先生成异常的情况下抛出

异常?


我使用的是结构化的异常处理即尝试捕获最终块与

a顶级捕获所有异常。但是,我希望能够在生成它们时捕获大多数.net异常。然后我就能够生成一个有价值的异常消息,并为此做点什么!因此上面的

问题。


如果有人想谈论他们首选的异常处理

方法我很满意。


谢谢,

马克

解决方案

嗨mag31,

据我所知,除非在某处记录异常,或者您可以访问原始源以查看该方法可能抛出的异常,否则您必须采用一般异常捕获,捕获(异常) ex)。


-

快乐编码!

Morten Wennevik [C#MVP]


谢谢Morten,它回答了我的问题,显然不是答案我想要:/ b $ b想要:-(


Mark


" Morten Wennevik"写道:

嗨mag31,

据我所知,除非在某处记录异常,或者您可以访问原始源以查看遇到的异常hod可能会抛出,你必须采取一般的例外捕获,捕获(Exception ex)。

-
快乐编码!
Morten Wennevik [C#MVP]


Imho,.NET有一个非常愚蠢的异常模型。没有办法告诉哪个

异常可以通过特定方法抛出。另外,例外hirarchy

似乎没有精心设计。


很多人一直告诉你使用catch(Exception)是不好的风格

你应该总是抓住特定的异常Iam认为在某些

位置是最好的方法。特别是当调用第三方

组件或包装winapi之类的东西如Process.Start或创建数据库

连接时,你永远不知道可以抛出哪种异常。

即使记录了例外情况,也有可能遗忘某些东西,或者在以后的版本中会抛出一个新的异常。

该组件或者你忘记捕获一个特定的异常。


当然,如果你调用int.Parse(),你肯定只能捕获FormatException

或从那里读取文件除了

IOException之外,别会抛出任何其他东西。但正如我所说,有时候更好地捕捉System.Exception,

只是为了确定。


-

cody


免费软件工具,游戏和幽默
http ://www.deutronium.de.vu || http://www.deutronium.tk

" mag31" ; <毫安*** @ discussions.microsoft.com> schrieb im Newsbeitrag

新闻:F2 ********************************** @ microsof t.com ...

有没有办法找出特定的.net函数是否会抛出
异常而不先生成异常?

我我正在使用结构化的异常处理,即try catch finally $ block $,其中顶级捕获全部为Exception。但是,我希望能够在生成它们时捕获大多数.net异常。然后,我将能够
生成一个有价值的异常消息并做一些事情!因此上面的问题是


如果有人想谈论他们首选的异常处理方法我很满意。

谢谢,
标记



Is there any way to find out if a particular .net function will throw an
exception without first generating the exception?

I am using structured exception handling i.e. try catch finally blocks with
a top level catch all for Exception. However, I would like to be able to
catch most .net exceptions when they are generated. I would then be able to
generate a valuable exception message and do something about it!!! Hence the
question above.

If anyone would like to talk about their preferred exception handling
methodology I am all ears.

Thanks,
Mark

解决方案

Hi mag31,

To my knowledge, unless the exception is documented somewhere, or you have access to the original source to see what exceptions that method may throw, you have to resort to a general exception catching, catch(Exception ex).

--
Happy Coding!
Morten Wennevik [C# MVP]


Thank you Morten, that answers my question, obviously not with the answer I
want :-(

Mark

"Morten Wennevik" wrote:

Hi mag31,

To my knowledge, unless the exception is documented somewhere, or you have access to the original source to see what exceptions that method may throw, you have to resort to a general exception catching, catch(Exception ex).

--
Happy Coding!
Morten Wennevik [C# MVP]



Imho, .NET has a very stupid exception model. There is not way telling which
exceptions can be thrown by a specifiy method. Also the Exception hirarchy
seems not to be well designed.

Lots of people keep telling you that using catch (Exception) is bad style
and you should always catch specific exception Iam the opinion that in some
places is the the best method. Especially when calling third party
components or wrapped winapi stuff like Process.Start or creating Database
connections, you never know which kind of exception can be thrown.
Even if exceptions are documented there a chance that something was
forgotten to document or in a later version a new exception is thrown in
that component or maybe you forget to catch one specific exception.

Sure, if you call int.Parse() you will certainly only catch FormatException
or when reading from a file there will nothing else be thrown except
IOException. But as I said sometimes its better to catch System.Exception,
just to be sure.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"mag31" <ma***@discussions.microsoft.com> schrieb im Newsbeitrag
news:F2**********************************@microsof t.com...

Is there any way to find out if a particular .net function will throw an
exception without first generating the exception?

I am using structured exception handling i.e. try catch finally blocks with a top level catch all for Exception. However, I would like to be able to
catch most .net exceptions when they are generated. I would then be able to generate a valuable exception message and do something about it!!! Hence the question above.

If anyone would like to talk about their preferred exception handling
methodology I am all ears.

Thanks,
Mark



这篇关于.Net例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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