Exception handling

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

Is it possible to set up C# compiler so that I would get a warning when I
miss any exception thrown by a framework?

Something like in Java, where the compiler forces you to handle all
exceptions.

Thanks,

Lubomir
 
Is it possible to set up C# compiler so that I would get a warning when I
miss any exception thrown by a framework?

There is an "unhandled exception" event. You can add a handler to that to
be notified whenever an exception occurs that isn't handled. Note that
the event doesn't allow you to handle the exception...it just tells you
that one happened.

I don't really see how the compiler can force you to handle all
exceptions, since there's no way to know at run-time whether an exception
will occur. Short of requiring a top-level exception handler in every
thread, that is. And of course, you could ensure that all exceptions are
handled by doing that (putting a top-level exception handler in every
thread).

Pete
 
Peter Duniho said:
There is an "unhandled exception" event. You can add a handler to that to
be notified whenever an exception occurs that isn't handled. Note that
the event doesn't allow you to handle the exception...it just tells you
that one happened.

I don't really see how the compiler can force you to handle all
exceptions, since there's no way to know at run-time whether an exception
will occur. Short of requiring a top-level exception handler in every
thread, that is. And of course, you could ensure that all exceptions are
handled by doing that (putting a top-level exception handler in every
thread).

The notion Lubomir is referring to is Java's idea of "checked
exceptions". The majority of exceptions are checked (things like
NullPointerException aren't) and methods can be declared to potentially
throw them. If you call a method which is declared to throw a
particular checked exception, you need to either catch it in your
method or declare that you might throw it too.

I used to be a big fan of checked exceptions, but over time I've come
to regard them as a failed experiment. It was worth doing, and I
suspect if checked exceptions had been used more sparingly they might
have been good in the long run, but in Java they're more of a curse
than a blessing now.
 
Jon Skeet said:
The notion Lubomir is referring to is Java's idea of "checked
exceptions". The majority of exceptions are checked (things like
NullPointerException aren't) and methods can be declared to potentially
throw them. If you call a method which is declared to throw a
particular checked exception, you need to either catch it in your
method or declare that you might throw it too.

I used to be a big fan of checked exceptions, but over time I've come
to regard them as a failed experiment. It was worth doing, and I
suspect if checked exceptions had been used more sparingly they might
have been good in the long run, but in Java they're more of a curse
than a blessing now.

Yes, Jon, that's what I was thinking about. The reason why I was asking my
question is, that in C# I have to deal with many API methods that thrown an
exception, and I wanted to have, an overview about which I am handling and
which I am not.
 
Yes, Jon, that's what I was thinking about. The reason why I was asking my
question is, that in C# I have to deal with many API methods that thrown an
exception, and I wanted to have, an overview about which I am handling and
which I am not.

Sorry, I forgot to give the definitive answer: C# (and .NET in general)
has nothing like this, I'm afraid.
 
[...]
I used to be a big fan of checked exceptions, but over time I've come
to regard them as a failed experiment. It was worth doing, and I
suspect if checked exceptions had been used more sparingly they might
have been good in the long run, but in Java they're more of a curse
than a blessing now.

Hmmm...interesting. Sounds a lot like the "const" issue in C++ (funny
coincidence :) ). Seems like a good idea, but winds up causing more
headaches than it solves.

Pete
 
Peter Duniho said:
[...]
I used to be a big fan of checked exceptions, but over time I've come
to regard them as a failed experiment. It was worth doing, and I
suspect if checked exceptions had been used more sparingly they might
have been good in the long run, but in Java they're more of a curse
than a blessing now.

Hmmm...interesting. Sounds a lot like the "const" issue in C++ (funny
coincidence :) ). Seems like a good idea, but winds up causing more
headaches than it solves.

I suspect if they started Java again from scratch, they could get it
right. It's too late now - there are so many exceptions which shouldn't
be checked that it's basically understood that exception handling is
messy. (The type hierarchy is horrible too - see
http://msmvps.com/blogs/jon.skeet/archive/2007/04/03/sheer-evil-
rethrowing-exceptions-in-java.aspx for some awful code.)

Likewise, I think it's too late for const to be introduced into .NET.
MS can't go around making everyone's code fail to compile this far into
the game, and even if the whole framework were fixed somehow, there's a
lot of code which effectively should be marked as const but which would
take an age to sort out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top