Easy one

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

Guest

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?
 
Patty O'Dors said:
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.
 
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 said:
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?
 
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
 
Oenone said:
Can't you just throw an ApplicationException then?

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

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