How to prevent compiling when an exception has not been handeled

S

shahoo

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?
Thanks.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

shahoo said:
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# does not have checked exceptions.

Arne
 
P

Peter Duniho

shahoo said:
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
 
J

Jon Skeet [C# MVP]

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. :)

Spot on. Checked exceptions are a somewhat failed experiment. I think
we may eventually want *something* like them, but not quite what Java
has. I used to be a big fan, but I'm not any more...
 
M

Michael S

Jon Skeet said:
Spot on. Checked exceptions are a somewhat failed experiment. I think
we may eventually want *something* like them, but not quite what Java
has. I used to be a big fan, but I'm not any more...

This is an interview with Anders Hejlsberg where he explains why there are
no checked exceptions in C#.

http://www.artima.com/intv/handcuffs.html

- Michael S
 
S

Samuel R. Neff

I've done only a little Java work and I really liked checked
exceptions. We had a situation recently where they would have help us
a lot in a c# project.

We have a standard library for database access code that we recently
developer. Part of the library uses IDataRecord.GetOrdinal() and the
interface doesn't explicitly say what happens when you call it with a
name that's not in the record.

Well, some implementations return -1 and others throw an exception.
They should be consistent.

This really is a documentation problem--the interface should say what
happens and then implementers should follow that. Checked exceptions
would be a way to force compliance.

Personally when I look at the docs the vast majority of the time is to
see what exceptions are thrown.

Sam


------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
 
U

UL-Tomten

[...] some implementations return -1 and others throw an exception.
[...] Checked exceptions would be a way to force compliance.

Sometimes returning -1 is just as legal as throwing an exception
((network) stream reading comes to mind), in which case checked
exceptions wouldn't really do anything for compliance. Carefully
thought-out design patterns do more to alleviate these problems than
do checked exceptions, _and_ don't come bundled with a hangover.
 
S

Samuel R. Neff

I don't agree that returning -1 and throwing an exception are both
legal when it's the same method of an interface, just different
concrete classes.

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.



[...] some implementations return -1 and others throw an exception.
[...] Checked exceptions would be a way to force compliance.

Sometimes returning -1 is just as legal as throwing an exception
((network) stream reading comes to mind), in which case checked
exceptions wouldn't really do anything for compliance. Carefully
thought-out design patterns do more to alleviate these problems than
do checked exceptions, _and_ don't come bundled with a hangover.
 
U

UL-Tomten

I don't agree [...]

Yeah, that's one of the reasons checked exceptions don't work in
practice right there. If we don't agree on the mechanics, compile-time
declarations aren't going to solve the original problem, which in this
case could be something like "I need to be certain I can gracefully
handle everything your code does".
 
S

shahoo

I am a Java fan myself and I like that feature too. But of my bad luck
I have to do a project in C# for my university course.
BTW, thanks for the help.
 

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

Top