Re: How to prevent compiling when an exception has not been handeled

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



shahoo wrote:
Hi,
As you know in Java, when a method is able to throw an exception the
programmer has to handle the exception otherwise the program will not
compile.
Is there anyway to do so in C#. I mean generate an error message for
each unhandled exception?

No. C# doesn't provide a way to advertise to the compiler which exceptions it could throw, so there's also no way for it to require that calling methods always catch exceptions that could be thrown.

Not having done any Java programming myself, I had never heard of that feature until someone else asked the same question a while back. I still have mixed feelings regarding whether I'd want to use that sort of requirement. On the one hand, it seems like a good idea with respect to making sure you always know what kind of exceptions you might need to handle. On the other hand, it sounds like it could lead to a lot of busy work, as you repeatedly bubble up possible exceptions. :)

Pete
.



Relevant Pages

  • Re: Should I use "if" or "try" (as a matter of speed)?
    ... Joel Spolsky might be a great C++ programmer, ... arguments about exceptions do not hold in Python. ...
    (comp.lang.python)
  • Re: Runtime error in Java 6
    ... ignored) abound in code that you can't do anything about, like the JDK classes. ... Try running some Java program under a debugger that breaks on all exceptions and you'll see what I mean. ... The API part throws Exceptions, in part because an API writer ... Unchecked Exceptions are for programmer error. ...
    (comp.lang.java.programmer)
  • Re: I cant believe how complex this Format SDK is...
    ... I've been a programmer since '80. ... provides me with an unbiased perspective on the docs and APIs. ... Overall I think the documentation is reasonably good, ... run my app in the debugger, such as first-chance exceptions when using ...
    (microsoft.public.windowsmedia.sdk)
  • Re: java.lang Exception hierarchy - why?
    ... >>RuntimeException has been placed as a subclass ... A program declaring such exceptions would be ... reference to unchecked and checked exceptions. ... > though this may be obvious to the programmer. ...
    (comp.lang.java.programmer)
  • Re: Real life cost of using exceptions for control flow?
    ... > your program operates in normal conditions" (and bad input is one of the ... single land line, DSL connection, and on all network connections between two ... If I am a reliable programmer, the second connection was already up ... spurious exceptions like out-of-space on the disk. ...
    (microsoft.public.dotnet.framework.performance)