开发者

No force of exception handling?

Programming Java in Eclipse, I'm used to having to deal with exceptions. In C# with VisualStudio it seems I can not say "throws exception" on a method... After a lot of coding I found lots of exceptions, and had to catch them as I found them during te开发者_如何转开发sting. I'd like to be forced to handling them, so that VisualStudio can say that here you need a catch or I'm not going to let you go on :) Is this possible?

Thanks


No. C# doesn't force you to handle exceptions. This was a concious design decision on the part of the language designers.

For details on why, see The Trouble With Checked Exceptions, where Anders Hejlsberg discusses why they aren't part of C#.


No there are no checked exceptions in C#. And the reasons are pretty simple:

1) they tend not to achieve there intended purpose because not every follows the recommended practice of either handling them explicitly or passing them up the chain. Instead what happens is that they are "eaten" by developers in the middle of the dev stack.
2) since they are naturally part of documentation rather than executable code, they should go into documentation and not clutter the code surface

3) C#/Framework documentation in dev tools are the ideal place for such documentation. Play around with the visual studio Object browser, browse to the method and you will see them mentioned in the lower pane.


No, it's not possible.

What you can do is to look up the .NET library method you are executing in MSDN: the exceptions which are expected to be thrown by the method are documented there. Afterwards, you can decide which ones you want to catch and which ones you don't want to handle here.


You can use various tools to find the exceptions. There's also this thing for .NET 4 called Code Contracts that help you enforce pre/post conditions and possibly avoid some exceptions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜