By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,219 Members | 2,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,219 IT Pros & Developers. It's quick & easy.

Easy one

P: n/a
Hi
I have a class which goes through its merry life as follows:
* it gets constructed,
* has some properties set,
* has some objects "added" by use of an "Add" method,
* has its "Go" method called,
* does its stuff, perhaps raising a few events,
* gets forgotten about and GCed.

When the "Go" method is called, it checks the parameters. They're not all passed as parameters to the Go method, but set before hand or "Add"ed.
My question is what type of exception should I raise if any of them are invalid, in the following scenarios:

* An object that has been added that is supposed to have sub-objects but doesn't?
* An object that has been added that is supposed to have equal numbers of two different types of subobjects but doesn't (think tables, source columns, dest columns)
* No objects have been added?

Does it actually matter if I use an 'InvalidOperationException' or an 'ArgumentNullException' or is there a better one?
Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Patty O'Dors <Pa********@discussions.microsoft.com> wrote:
I have a class which goes through its merry life as follows:
* it gets constructed,
* has some properties set,
* has some objects "added" by use of an "Add" method,
* has its "Go" method called,
* does its stuff, perhaps raising a few events,
* gets forgotten about and GCed.

When the "Go" method is called, it checks the parameters. They're not
all passed as parameters to the Go method, but set before hand or
"Add"ed.
My question is what type of exception should I raise if any of them
are invalid, in the following scenarios:


I think InvalidOperationException is the one to go for unless the
caller has passed a null parameter (which should be non-null) to the
actual method.

The description of InvalidOperationException exactly matches your
problem, I think.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 20 '05 #2

P: n/a
There's no hard and fast rule that says you have to throw either of these
exceptions. You can extend ApplicationException and create exception
classes that match your exception situation perfectly if the .NET exceptions
don't quite fit the bill.

You could create:
MissingSubObjectException
SubObjectMismatchException
EmptyObjectCollectionException

Those are just a stab in the dark for names feel free to use whatever you
like. :-)

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.

"Patty O'Dors" <Pa********@discussions.microsoft.com> wrote in message
news:EF**********************************@microsof t.com...
Hi
I have a class which goes through its merry life as follows:
* it gets constructed,
* has some properties set,
* has some objects "added" by use of an "Add" method,
* has its "Go" method called,
* does its stuff, perhaps raising a few events,
* gets forgotten about and GCed.

When the "Go" method is called, it checks the parameters. They're not all passed as parameters to the Go method, but set before hand or "Add"ed. My question is what type of exception should I raise if any of them are invalid, in the following scenarios:
* An object that has been added that is supposed to have sub-objects but doesn't? * An object that has been added that is supposed to have equal numbers of two different types of subobjects but doesn't (think tables, source columns,
dest columns) * No objects have been added?

Does it actually matter if I use an 'InvalidOperationException' or an

'ArgumentNullException' or is there a better one?
Nov 20 '05 #3

P: n/a
That'd be very sweet, but I'd get no actual gain out of doing that work.

I don't need my exceptions to DO anything, other than to be thrown.

Cheers


"C Addison Ritchie" wrote:
There's no hard and fast rule that says you have to throw either of these
exceptions. You can extend ApplicationException and create exception
classes that match your exception situation perfectly if the .NET exceptions
don't quite fit the bill.

You could create:
MissingSubObjectException
SubObjectMismatchException
EmptyObjectCollectionException

Those are just a stab in the dark for names feel free to use whatever you
like. :-)

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.

"Patty O'Dors" <Pa********@discussions.microsoft.com> wrote in message
news:EF**********************************@microsof t.com...
Hi
I have a class which goes through its merry life as follows:
* it gets constructed,
* has some properties set,
* has some objects "added" by use of an "Add" method,
* has its "Go" method called,
* does its stuff, perhaps raising a few events,
* gets forgotten about and GCed.

When the "Go" method is called, it checks the parameters. They're not all

passed as parameters to the Go method, but set before hand or "Add"ed.
My question is what type of exception should I raise if any of them are

invalid, in the following scenarios:

* An object that has been added that is supposed to have sub-objects but

doesn't?
* An object that has been added that is supposed to have equal numbers of

two different types of subobjects but doesn't (think tables, source columns,
dest columns)
* No objects have been added?

Does it actually matter if I use an 'InvalidOperationException' or an

'ArgumentNullException' or is there a better one?

Nov 20 '05 #4

P: n/a
Patty O'Dors wrote:
That'd be very sweet, but I'd get no actual gain out of doing that
work.


Can't you just throw an ApplicationException then?

--

(O)enone
Nov 20 '05 #5

P: n/a
Oenone <no***@nowhere.com> wrote:
Patty O'Dors wrote:
That'd be very sweet, but I'd get no actual gain out of doing that
work.


Can't you just throw an ApplicationException then?


Why throw that when InvalidOperationException exactly matches the
description of the problem?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.