473,549 Members | 2,726 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding Checked Exceptions to C#

I drank too much coffee last night and came up with a suggestion about
how
to add checked exceptions to C#

http://www.geocities.com/jeff_louie/OOP/oop14.htm

Comments expected <g>

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
9 1848
Jeff,

Exception handling seems to crop up at least once a month. This is probably
going to be another one of those long threads.

I agree with checking conditions before trying to perform an action (where
realistically feasible) but I have little need for anymore than one
Exception class. All I ever do with an exception is display a message to the
user (depending on the type of application) and log it. If I need to take
different cause within code then I check for the condition before hand, in
which case I cannot normally do this in a generic class which wraps up
common functionality.

if (!File.Exists(f ile))
{
throw new FileNotFoundExc eption(filePath );
}
Your example code checks if the file exists but simply throws an exception,
how does this differ to what the framework does? Yes it performs a close
before an open but I prefer to do this explicitly within the main calling
code. For me trying to open a closed file should throw an exception not hide
the bug in my code where I failed to do so
came up with a suggestion about how to add checked exceptions to C#
In any case your proposal does not really fall into the category of
"checked" exceptions. I seem to remember seeing something in
www.CodeProject.com where somebody achieved this by using Attributes.

But perhaps I have missed your point...

Phil...

"Jeff Louie" <je********@yah oo.com> wrote in message
news:eI******** ******@TK2MSFTN GP11.phx.gbl...I drank too much coffee last night and came up with a suggestion about
how
to add checked exceptions to C#

http://www.geocities.com/jeff_louie/OOP/oop14.htm

Comments expected <g>

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
If I get it right your proposal is to define at least 4 different exceptions
for each class?

And what about interfaces?

class MyThread : IRunnable
{
public void Run(){}
}

What exceptions will run throw? I assume both IRunnableExcept ion AND
MyThreadExcepti on.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
"Jeff Louie" <je********@yah oo.com> schrieb im Newsbeitrag
news:eI******** ******@TK2MSFTN GP11.phx.gbl...
I drank too much coffee last night and came up with a suggestion about
how
to add checked exceptions to C#

http://www.geocities.com/jeff_louie/OOP/oop14.htm

Comments expected <g>

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3

"Jeff Louie" <je********@yah oo.com> wrote in message
news:eI******** ******@TK2MSFTN GP11.phx.gbl...
I drank too much coffee last night and came up with a suggestion about
how
to add checked exceptions to C#

http://www.geocities.com/jeff_louie/OOP/oop14.htm

Comments expected <g>


As best I can tell, this catch by contract suffers from the same problem I
have with regular checked exceptions.

Why is it that I would have to catch MyClassFileNotF oundException and
YourClassFileNo tFoundException simply because of artificial exception
contracts? Or worse MyClassYourClas sFileNotFoundEx ception and
YourClassMyClas sFileNotFoundEx ception.

It seems to me you may actually end up with several dozen exceptions that
mean the same thing, but just have different points of origin.
Nov 16 '05 #4

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:eG******** ******@TK2MSFTN GP09.phx.gbl...

"Jeff Louie" <je********@yah oo.com> wrote in message
news:eI******** ******@TK2MSFTN GP11.phx.gbl...
I drank too much coffee last night and came up with a suggestion about
how
to add checked exceptions to C#

http://www.geocities.com/jeff_louie/OOP/oop14.htm

Comments expected <g>


As best I can tell, this catch by contract suffers from the same problem I
have with regular checked exceptions.

Why is it that I would have to catch MyClassFileNotF oundException and
YourClassFileNo tFoundException simply because of artificial exception
contracts? Or worse MyClassYourClas sFileNotFoundEx ception and
YourClassMyClas sFileNotFoundEx ception.

It seems to me you may actually end up with several dozen exceptions that
mean the same thing, but just have different points of origin.


Now, for the plus sides.

I do think that pre|post-conditions and exceptions could be tied together.
I've argued the point before, and I personally would like a more passive
checked exception solution. Somethign that defines input, state, and perhaps
output constraints as well as a response(except ion) for invalid values, but
that does not enforce any actions. Basically a DbC implementation that is
cleanly tied to C# combined with checked exceptions, but built more for
better tools than for definate correctness(I don't really believe such a
thing is possible, thus its silly to try to pretend it is).
Nov 16 '05 #5

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:e5******** ******@tk2msftn gp13.phx.gbl...

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:eG******** ******@TK2MSFTN GP09.phx.gbl...

"Jeff Louie" <je********@yah oo.com> wrote in message
news:eI******** ******@TK2MSFTN GP11.phx.gbl...
I drank too much coffee last night and came up with a suggestion about
how
to add checked exceptions to C#

http://www.geocities.com/jeff_louie/OOP/oop14.htm

Comments expected <g>


As best I can tell, this catch by contract suffers from the same problem
I have with regular checked exceptions.

Why is it that I would have to catch MyClassFileNotF oundException and
YourClassFileNo tFoundException simply because of artificial exception
contracts? Or worse MyClassYourClas sFileNotFoundEx ception and
YourClassMyClas sFileNotFoundEx ception.

It seems to me you may actually end up with several dozen exceptions that
mean the same thing, but just have different points of origin.


Now, for the plus sides.

I do think that pre|post-conditions and exceptions could be tied together.
I've argued the point before, and I personally would like a more passive
checked exception solution. Somethign that defines input, state, and
perhaps output constraints as well as a response(except ion) for invalid
values, but '


Err, tht should be, "input, output, and perhaps state"
Nov 16 '05 #6
Hi Cody... Well there are three proposals. If all three are implemented
than any class that throws a checked exception would need to define at
least three class level exceptions such as:
MyClassExceptio n
PreConditionExc eption
FileNotFoundExc eption

A more simple approach would be just to do
MyClassExceptio n
FileNotFoundExc eption

The framework would define System.CheckedE xception

I had not thought about interfaces, but if an interface declared that a
checked exception could be thrown using the throws keyword I suspect
that the implementing class would define the exception hierarchy as
above. Existing exceptions would not be affected since they do not
inherit from System.CheckedE xception and are not declared in a method
using a throws keyword.

If a user did not want to throw checked exceptions, the user would not
need to declare any class level checked exceptions, but would need to
catch and handle any checked exceptions thrown by suppliers to the
class.

Regards,
Jeff
If I get it right your proposal is to define at least 4 different

exceptions for each class?

And what about interfaces?<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #7
Phil...I rewrote the chapter a bit, so hopefully my points are clearer.

1) David Abrahams has argued that one person's exception is another
person's error, making the distinction less than useful.... In this
chapter I suggest implementing a systematic approach to exceptions
discussed by Herb Sutter that removes such ambiguity.

2) My second suggestion is to add checked exceptions to C# by requiring
the compiler to enforce what I am calling catch by contract. Any class
that throws a checked exception must declare a class level base
exception type and all CHECKED exceptions thrown by the class must
inherit from this class level base exception.

Versioning and scalability concerns are addressed by insuring that a
class level base exception exist and that all checked exceptions inherit
from this class level exception. Exceptional method calls can conclude
with catch(SomeBaseC lassException e) {}. Catch by contract should
suppress the urge to catch{}!

3) Finally, I suggest that Herb Sutter's concept of an error can be
captured in the exception hierarchy.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #8
Hi Daniel.. or is Dan OK?
With regular checked exceptions you can just explicitly declare that the
client method throws the exception, freeing you from having to catch it.
It will just bubble up. Or you can do a catch{} <g>, which unfortunately
happens more than we admit. With catch by contract, bubbling up a
checked exception would require more work: catch, convert and rethrow.
So I agree, catch by contract is meant for exceptions that are going to
be handled by the client, Else use regular, hence unchecked, exceptions.

Regards,
Jeff
As best I can tell, this catch by contract suffers from the same

problem I have with regular checked exceptions.

Why is it that I would have to catch MyClassFileNotF oundException and
YourClassFileNo tFoundException simply because of artificial exception
contracts?<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #9

"Jeff Louie" <je********@yah oo.com> wrote in message
news:eK******** ******@TK2MSFTN GP11.phx.gbl...
Hi Daniel.. or is Dan OK?
Dan is fine, ;)
With regular checked exceptions you can just explicitly declare that the
client method throws the exception, freeing you from having to catch it.
It will just bubble up. Or you can do a catch{} <g>, which unfortunately
happens more than we admit. With catch by contract, bubbling up a
checked exception would require more work: catch, convert and rethrow.
So I agree, catch by contract is meant for exceptions that are going to
be handled by the client, Else use regular, hence unchecked, exceptions.
My concern is more in, say, class A contains class B. When one of class B's
methods fails, class A would have to wrap those into exceptions that are
valid with A(In other words ABException, lol). Thus, you could end up
writing alot of:

try
{
... //do something
}
catch (CheckedExcepti on ce)
{
throw new MyCheckedExcept ion(ce);
}

just to maintain the exception hiearchy. Both exceptions are technically
something the utlimate client must handle, but who's to say who the ultimate
client is? While in some cases the client may be one step away, would you
want to constrict your code so that it is only valid within the context of
immediate use? I don't think I would.
So, how do you decide when to use unchecked?
Regards,
Jeff
As best I can tell, this catch by contract suffers from the same

problem I have with regular checked exceptions.

Why is it that I would have to catch MyClassFileNotF oundException and
YourClassFileNo tFoundException simply because of artificial exception
contracts?<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
2866
by: OvErboRed | last post by:
I just read a whole bunch of threads on microsoft.public.dotnet.* regarding checked exceptions (the longest-running of which seems to be <cJQQ9.4419 $j94.834878@news02.tsnz.net>. My personal belief is that checked exceptions should be required in .NET. I find that many others share the same views as I do. It is extremely frustrating to have...
9
1463
by: F. GEIGER | last post by:
Hi everybody, I like checked exceptions as provided by Java. No, I don't want to start a thread on that, but ask, if anyone knows of a tool for that. It may be stand-alone, but being plugable into MSVS would ease its use, I guess. Many thanks in advance and best regards Franz GEIGER
0
7524
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7960
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7475
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7812
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5089
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1944
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.