Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 testing. 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

like image 526
Johannes Avatar asked Oct 19 '10 20:10

Johannes


Video Answer


2 Answers

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#.

like image 82
Reed Copsey Avatar answered Oct 06 '22 09:10

Reed Copsey


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.

like image 25
explorer Avatar answered Oct 06 '22 11:10

explorer