何时创建自己的异常类? [英] When to create your own exception class?

查看:58
本文介绍了何时创建自己的异常类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在与另一位程序员讨论关于

thrownig异常的最佳实践。我想在自己的命名空间中创建一个包含许多

不同异常类的丰富类库,因此很容易找到带有intellisense的
。他想抛出异常或其中一个构建在

异常中。你是否有一些很好的论据来创建你自己的异常

类,即使它们只是一个类型的强名称和相同的

作为System.Exception(来自他们继承的女巫) )?我们需要类型strongname

来编写特定的catch块吗?


您的意见是赞赏的。


我这里有关于例外的内容:

http: //www.codeproject.com/dotnet/ex...tpractices.asp

http://www.ftponline.com/vsm/2003_05/online/wagner/

http://www.codeguru.com/Csharp/Cshar .. .cle.php / c8605 /
http://www.dotnetspider.com/technology/kbpages/931.aspx
http://www.dotnetspider.com/technolo...x?SampleId=933
http://www.ondotnet.com/pub/a/dotnet..._handling.html <

http://www.c-sharpcorner .com / Article ... gInCSKS004.asp

http://vsdntips.com/download/dotNet%...rds-sample.pdf

http://smccd.net/accounts/tilmann/ci...ndout%20S7.pdf

http:// builder .com.com / 5100-6387-1045391.html

http://www.javaspecialists.co.za/archive/Issue089.html

http://www.c2.com/cgi/wiki?ConvertExceptions

http://www.c2.com/cgi/wiki?JavaIdioms

http:// www.javaworld.com/javatips/jw-javatip91_p.html

http://www.onjava.com/pub/a/onjava/2...xceptions.html

http://www.onjava.com/pub/a/onjava/2...ng.html?page=1
http://msdn.microsoft.com/library/de...gstatement.asp

亲切的问候,

Allan Ebdrup

Hi
I''m having a discussion with another programmer about a best practice for
thrownig exceptions. I want to create a rich class library with many
different exception classes in their own namespace, so they are easy to find
with intellisense. He wants to just throw Exception or one of the build in
exceptions. Do you have some good arguments for creating your own exception
classes even though they are little more than a type strongname and the same
as System.Exception (from witch they inherit)? We need the type strongname
to write specific catch blocks right?

Your input is appreciated.

I''ve read about exceptions here:

http://www.codeproject.com/dotnet/ex...tpractices.asp

http://www.ftponline.com/vsm/2003_05/online/wagner/

http://www.codeguru.com/Csharp/Cshar...cle.php/c8605/

http://www.dotnetspider.com/technology/kbpages/931.aspx

http://www.dotnetspider.com/technolo...x?SampleId=933

http://www.ondotnet.com/pub/a/dotnet..._handling.html

http://www.c-sharpcorner.com/Article...gInCSKS004.asp

http://vsdntips.com/download/dotNet%...rds-sample.pdf

http://smccd.net/accounts/tilmann/ci...ndout%20S7.pdf

http://builder.com.com/5100-6387-1045391.html

http://www.javaspecialists.co.za/archive/Issue089.html

http://www.c2.com/cgi/wiki?ConvertExceptions

http://www.c2.com/cgi/wiki?JavaIdioms

http://www.javaworld.com/javatips/jw-javatip91_p.html

http://www.onjava.com/pub/a/onjava/2...xceptions.html

http://www.onjava.com/pub/a/onjava/2...ng.html?page=1

http://msdn.microsoft.com/library/de...gstatement.asp

Kind Regards,
Allan Ebdrup

推荐答案

" Allan Ebdrup" <共**** @ ofir.com> écritdansle message de news:

u
"Allan Ebdrup" <co****@ofir.com> a écrit dans le message de news:
u


************** @ TK2MSFTNGP10.phx.gbl ...
**************@TK2MSFTNGP10.phx.gbl...
我正在与另一位程序员讨论关于
thrownig异常的最佳实践。我想在自己的命名空间中创建一个包含许多不同异常类的丰富类库,因此使用intellisense很容易找到
。他只想抛出异常或其中一个构建
异常。你是否有一些很好的参数来创建你自己的
异常类,即使它们只是一个类型的强名,而
与System.Exception相同(来自他们继承的巫婆)?我们需要类型strongname
来编写特定的catch块吗?
I''m having a discussion with another programmer about a best practice for
thrownig exceptions. I want to create a rich class library with many
different exception classes in their own namespace, so they are easy to find with intellisense. He wants to just throw Exception or one of the build in
exceptions. Do you have some good arguments for creating your own exception classes even though they are little more than a type strongname and the same as System.Exception (from witch they inherit)? We need the type strongname
to write specific catch blocks right?




自定义异常不仅仅是一个强名称,它们还可以携带额外的

数据和对象,使您能够对异常采取适当的行动。


例如


我有价值键入框架,允许我在

之前和设置或更改对象的属性之后添加自定义验证。如果验证

失败,我会引发一个ValueTypeValidation异常,其中包含对属性的原始值和更改值的引用

。然后我可以使用那些

对象向用户显示一条消息,说明原因和价值所涉及的价值,而不是其中一个无用的一个错误已经出现。输入

条消息。


Joanna


-

Joanna Carter

顾问软件工程师



Custom Exceptions are not just a "strong name", they can also carry extra
data and objects to enable you to take appropriate action on the exception.

e.g.

I have a Value Type framework that allows me to add custom validation before
and after a property of an object is set or changed. If the validation
fails, I raise an ValueTypeValidation exception that includes a reference to
both the original and changed values of the property. I can then use those
objects to display a message to the user stating the reason and values
involved, instead of one of those useless "An eror has ocurred" type
messages.

Joanna

--
Joanna Carter
Consultant Software Engineer


关于例外的实际观点是他们应该:


- 提供友好的以非技术术语向用户发送关于

发生的信息,为什么以及最重要的,如何从中恢复

- 向开发人员提供的非常详细的技术报告,隐藏在详细信息后面

按钮或在发送报告时包含。


因此,如果您在框架中找不到合适的例外来传达

特定信息或提供带有数据的详细消息(例如导致无效参数异常类型的

值),创建一个新的

异常类并添加一些属性。如果你要重新抛出一个

异常,你可能想要封装它(使用InnerException字段)

来提供更好的消息或数据,但不要忘记包括您作为开发人员收到的报告中的内部

异常。


-

祝你好运,


Carlos J. Quintero


MZ-Tools:Visual Studio .NET,VB6,VB5和VBA的生产力插件

您可以更快地编码,设计和记录。

加载项开发人员的免费资源:
http://www.mztools.com


" Allan Ebdrup" <共**** @ ofir.com> escribióenel mensaje

news:u
The actual point about exceptions is that they should:

- Provide a friendly message to the user in non-technical terms about what
happened, why and most important, how to recover from it
- A very detailed technical report to the developer, hidden behind a Details
button or included when sending a report.

So, if you don′t find a suitable exception in the framework to convey
specific information or to provide a detailed message with data (such as the
value that caused an "invalid argument" exception type), create a new
exception class and add it some properties. If you are rethrowing an
exception, you may want to encapsulate it (using the InnerException field)
to provide a better message or data, but don′t forget to include the inner
exception in the report that you receive as developer.

--
Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

"Allan Ebdrup" <co****@ofir.com> escribió en el mensaje
news:u


这篇关于何时创建自己的异常类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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