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 orshould I derive my own exceptions from ApplicationException?In general, it is not clear to me when to use the predefined exceptions andwhen to create new ones by deriving from ApplicationException.And a last question: what exactly is the difference betweenNotImplementedException and NotSupportedException?--cody[Freeware, Games and Humor] www.deutronium.de.vu || www.deutronium.tk 解决方案The problem is, maybe today I develop a class which throws aInvalidOperationExceptionif the User is trying to do something he can''t do at the moment. Now laterafter 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 exceptionfrom ApplicationException now.The code still compiles fine but if the exception is actually throw nobodycan catch it!You can say what you want, but catching "Exception" instead of a specificexception is always more secure as long as you take some action in the catchclause and do not swallow it.Nobody can tell me which kind of Exception a method can throw, even if thedocs tell you that this method will only throw FooException, the docs can beoutdated or incomplete. A programmer of a string format might be decided tothrow an ArgumentException instead of an IndexOutOfBoundsException if theformatstring is to long. Or maybe later he recognizes it is better to throwcan FormatException instead.I know catching a generic Excpeption is cosidered as sloppy coding, but ifIam not 100% sure which exception to catch, I catch all. Especially withInterOp you can never sure what happens,in this case I additionally use a catch{} that will catch non-CLR exeptionsas well.--codyFreeware Tools, Games and Humour http://www.deutronium.de.vu || http://www.deutronium.tk 这篇关于抛出哪种例外情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-01 12:27