抛出哪种例外情况? [英] which kind of Exceptions to throw?

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

问题描述

如果用户按下某个按钮,我会调用一个方法。

如果对象处于不允许调用该方法的状态,

我该怎么办?我应该更好地抛出InvalidOperationException或

我应该从ApplicationException派生自己的异常吗?


一般来说,我不清楚何时使用预定义的异常和

何时通过从ApplicationException派生来创建新的。


最后一个问题:
$ b $之间究竟有什么区别? b NotImplementedException和NotSupportedException?


-

cody


[免费软件,游戏和幽默]
www.deutronium.de.vu || www.deutronium.tk

解决方案

你好Cody:


至于两者之间的区别,我相信区别是

通常被认为是NotSupportedExcpetion在派生的

类中使用,其中的功能没有内置到基类中。我实际上

希望MS比较经常使用它b / c,例如,CF中的大多数控件都有一个''点击''事件,你可以连线它会进行编译,

当你点击它时它就不会工作b / c它们没有实现它。这个

会导致一些逻辑问题。


至于使用无效或编写自己的...取决于。您需要多少信息

?如果您有一些内部数据,例如你想知道

,你可以把它包含在expetion的消息(或其他一些

属性)中,这样你就可以了可以得到一些定制的信息。在大多数情况下,

如果您需要细粒度的

功能或信息,我建议您使用自定义异常。另一个时候你的对象可能需要一些特殊的功能。例如,如果您有一个LogIn类,那么如果有人试图使用

受保护的登录帐户(比如'sa'),那么你可能想要实现特殊情况。 '在SQL Server中)或者如果他们没有

验证x次。你可以捕获这个特别是为了突破你的日常工作并记录它或其他任何东西。


总的来说我认为你可以轻松过关得出你自己的
例外情况,你可以随身携带

。但是如果你需要特定的信息或者特殊情况下

逻辑适合是干净的,那么实现你自己的是合法的。


HTH,


Bill

" cody" < PL ********** @ gmx.de>写在消息

新闻:ud ************** @ TK2MSFTNGP09.phx.gbl ...

我有一个方法,如果用户按下某个按钮,则会被调用。
如果对象处于不允许调用该方法的状态,
我该怎么办?我是否应该更好地抛出InvalidOperationException或
我应该从ApplicationException派生自己的异常?

一般来说,我不清楚何时使用预定义的异常
以及何时使用通过从ApplicationException派生来创建新的。

最后一个问题:
NotImplementedException和NotSupportedException有什么区别?

-
cody

[免费软件,游戏和幽默]
www.deutronium .de.vu || www.deutronium.tk



你好Cody:


至于两者之间的区别,我认为区别是

一般认为是NotSupportedExcpetion用于派生的

类,其中的功能没有内置到基类中。我实际上

希望MS比较经常使用它b / c,例如,CF中的大多数控件都有一个''点击''事件,你可以连线它会进行编译,

当你点击它时它就不会工作b / c它们没有实现它。这个

会导致一些逻辑问题。


至于使用无效或编写自己的...取决于。您需要多少信息

?如果您有一些内部数据,例如你想知道

,你可以把它包含在expetion的消息(或其他一些

属性)中,这样你就可以了可以得到一些定制的信息。在大多数情况下,

如果您需要细粒度的

功能或信息,我建议您使用自定义异常。另一个时候你的对象可能需要一些特殊的功能。例如,如果您有一个LogIn类,那么如果有人试图使用

受保护的登录帐户(比如'sa'),那么你可能想要实现特殊情况。 '在SQL Server中)或者如果他们没有

验证x次。你可以捕获这个特别是为了突破你的日常工作并记录它或其他任何东西。


总的来说我认为你可以轻松过关得出你自己的
例外情况,你可以随身携带

。但是如果你需要特定的信息或者特殊情况下

逻辑适合是干净的,那么实现你自己的是合法的。


HTH,


Bill

" cody" < PL ********** @ gmx.de>写在消息

新闻:ud ************** @ TK2MSFTNGP09.phx.gbl ...

我有一个方法,如果用户按下某个按钮,则会被调用。
如果对象处于不允许调用该方法的状态,
我该怎么办?我是否应该更好地抛出InvalidOperationException或
我应该从ApplicationException派生自己的异常?

一般来说,我不清楚何时使用预定义的异常
以及何时使用通过从ApplicationException派生来创建新的。

最后一个问题:
NotImplementedException和NotSupportedException有什么区别?

-
cody

[免费软件,游戏和幽默]
www.deutronium .de.vu || www.deutronium.tk



>总的来说,我认为你可以很容易地得到你自己的

例外,你可以肯定得到
带走的东西。但是如果你需要特定的信息或者特殊的情况下
逻辑适合是干净的,那么实现你自己的是合法的。



问题是,也许今天我开发了一个类抛出一个

InvalidOperationException

如果用户正在尝试做他目前无法做的事情。现在稍后

经过一段时间的开发后,

我认识到我需要在我的例外情况下携带soem数据。


所以怎么办?当我现在使用派生异常时,我会破坏所有客户端代码。现在,来自ApplicationException的


代码仍然可以正常编译,但如果异常实际上没有人

可以抓住它!


你可以说出你想要的东西,但却能捕捉到异常。而不是特定的

异常总是更安全,只要你在捕获

子句中采取一些行动并且不要吞下它。

没有人能告诉我一个方法可以抛出哪种异常,即使

文档告诉你这个方法只会抛出FooException,文档可能是b / b
过时或不完整。如果

格式字符串为long,则可能会决定字符串格式的程序员

抛出ArgumentException而不是IndexOutOfBoundsException。或许以后他认识到最好抛出

而不是FormatException。


我知道捕获一个通用的Excpeption被认为是草率的编码,但是如果

我不能100%确定捕获的异常,我抓住了所有。特别是使用

InterOp你永远无法确定会发生什么,

在这种情况下我另外使用一个捕获{}来捕获非CLR exeptions




-

cody


免费软件工具,游戏和幽默
< a rel =nofollowhref =http://www.deutronium.de.vutarget =_ blank> http://www.deutronium.de.vu || http://www.deutronium.tk

I have a method that gets called if the user presses a certain button.
If the object is in a state that doesn''t allow the calling of that method,
what should I do? Should I better throw an InvalidOperationException or
should I derive my own exceptions from ApplicationException?

In general, it is not clear to me when to use the predefined exceptions and
when to create new ones by deriving from ApplicationException.

And a last question: what exactly is the difference between
NotImplementedException and NotSupportedException?

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

解决方案

Hi Cody:

As far as the distinction between the two go, I believe the distinction is
generally considered to be that NotSupportedExcpetion is used in derived
classes where the functionality isn''t built into the base class. I actually
wish MS used it more often b/c for instance, most every control in the CF
for instance has a ''Click'' event and you can wire it in and it will compile,
it just won''t work when you click on it b/c they didn''t implement it. This
can lead to some logic problems.

As far as using Invalid or writing your own..it depends. How much info will
you need? If you have some internal data for instance that you want to know
about, you can include it in the excpetion''s message (or some other
property) so you can get some customized information. For the most part,
I''d recommend using custom exceptions when you need finely grained
functionality or informationd. Another time is when your objects may need
some special functionality. For instance, if you had a LogIn class, you
might want to implement a special case scenario if someone tries to use a
protected login account (like ''sa'' in SQL Server) or if they fail to
authenticate x number of times. You can trap this particularly to break out
of your routine and log it or whatever else.

By and large I think you could easily get by without ever deriving an
exception of your own and they are something you can definitely get carried
away with. But if you need specific info or have special cases where the
logic fit is clean, then implementing your own is legit.

HTH,

Bill
"cody" <pl*************************@gmx.de> wrote in message
news:ud**************@TK2MSFTNGP09.phx.gbl...

I have a method that gets called if the user presses a certain button.
If the object is in a state that doesn''t allow the calling of that method,
what should I do? Should I better throw an InvalidOperationException or
should I derive my own exceptions from ApplicationException?

In general, it is not clear to me when to use the predefined exceptions and when to create new ones by deriving from ApplicationException.

And a last question: what exactly is the difference between
NotImplementedException and NotSupportedException?

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk



Hi Cody:

As far as the distinction between the two go, I believe the distinction is
generally considered to be that NotSupportedExcpetion is used in derived
classes where the functionality isn''t built into the base class. I actually
wish MS used it more often b/c for instance, most every control in the CF
for instance has a ''Click'' event and you can wire it in and it will compile,
it just won''t work when you click on it b/c they didn''t implement it. This
can lead to some logic problems.

As far as using Invalid or writing your own..it depends. How much info will
you need? If you have some internal data for instance that you want to know
about, you can include it in the excpetion''s message (or some other
property) so you can get some customized information. For the most part,
I''d recommend using custom exceptions when you need finely grained
functionality or informationd. Another time is when your objects may need
some special functionality. For instance, if you had a LogIn class, you
might want to implement a special case scenario if someone tries to use a
protected login account (like ''sa'' in SQL Server) or if they fail to
authenticate x number of times. You can trap this particularly to break out
of your routine and log it or whatever else.

By and large I think you could easily get by without ever deriving an
exception of your own and they are something you can definitely get carried
away with. But if you need specific info or have special cases where the
logic fit is clean, then implementing your own is legit.

HTH,

Bill
"cody" <pl*************************@gmx.de> wrote in message
news:ud**************@TK2MSFTNGP09.phx.gbl...

I have a method that gets called if the user presses a certain button.
If the object is in a state that doesn''t allow the calling of that method,
what should I do? Should I better throw an InvalidOperationException or
should I derive my own exceptions from ApplicationException?

In general, it is not clear to me when to use the predefined exceptions and when to create new ones by deriving from ApplicationException.

And a last question: what exactly is the difference between
NotImplementedException and NotSupportedException?

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk



> By and large I think you could easily get by without ever deriving an

exception of your own and they are something you can definitely get carried away with. But if you need specific info or have special cases where the
logic fit is clean, then implementing your own is legit.


The problem is, maybe today I develop a class which throws a
InvalidOperationException
if the User is trying to do something he can''t do at the moment. Now later
after some time of development,
I recognize that I need to carry soem data in my exception.

So now what? I would break all client code when I use a derived exception
from ApplicationException now.
The code still compiles fine but if the exception is actually throw nobody
can catch it!

You can say what you want, but catching "Exception" instead of a specific
exception is always more secure as long as you take some action in the catch
clause and do not swallow it.
Nobody can tell me which kind of Exception a method can throw, even if the
docs tell you that this method will only throw FooException, the docs can be
outdated or incomplete. A programmer of a string format might be decided to
throw an ArgumentException instead of an IndexOutOfBoundsException if the
formatstring is to long. Or maybe later he recognizes it is better to throw
can FormatException instead.

I know catching a generic Excpeption is cosidered as sloppy coding, but if
Iam not 100% sure which exception to catch, I catch all. Especially with
InterOp you can never sure what happens,
in this case I additionally use a catch{} that will catch non-CLR exeptions
as well.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk


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

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