Custom Exception

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

Guest

I have created a class with the following constructor:

Public Sub New(ByVal ID As Integer)
'Use ID to get info from data store and set all properties
End Sub

I need to somehow handle an invalid ID being passed to the constructor. By
invalid, I mean one that does not have an associated record in the data
store. I believe that throwing an exception would be the best way to handle
this, but am not sure. After reading quite a few articles it seems exceptions
should only be used when you don't expect something to happen, but yet it is
still possible. Theoretically this constructor should never be passed an
invalid ID, but it's definitely possible. I have also read that it is good
practice to derive my own custom Exception class from ApplicationException. I
was thinking about making a "NoMatchingRecordFoundException" class. Any help
would be appreciated. Also, if I do start creating these custom exceptiions
what is the best way to do this so I can implement them in all of my projects?
 
<"=?Utf-8?B?U2hhd24gQmVyZw==?=" <Shawn
I have created a class with the following constructor:

Public Sub New(ByVal ID As Integer)
'Use ID to get info from data store and set all properties
End Sub

I need to somehow handle an invalid ID being passed to the constructor. By
invalid, I mean one that does not have an associated record in the data
store. I believe that throwing an exception would be the best way to handle
this, but am not sure. After reading quite a few articles it seems exceptions
should only be used when you don't expect something to happen, but yet it is
still possible. Theoretically this constructor should never be passed an
invalid ID, but it's definitely possible. I have also read that it is good
practice to derive my own custom Exception class from ApplicationException. I
was thinking about making a "NoMatchingRecordFoundException" class. Any help
would be appreciated. Also, if I do start creating these custom exceptiions
what is the best way to do this so I can implement them in all of my projects?

No, in this case the best thing to throw would be
InvalidArgumentException, I believe. Use standard exceptions when
they're available.

Note that ApplicationException is deprecated in .NET 2.0 - MS realised
that it basically doesn't add anything, it just makes for a deeper
hierarchy.
 
Jon,

Thanks for the reply. I have thought about using InvalidArgumentException,
but did not think it was right for this situation seeing that the argument
passed is valid in itself, there is just no data that it is associated with.
I guess it could go both ways, but that's how I interpreted it.

Where can I see a list of all of these pre-built exception classes that I
can choose from?

Also, I was not aware that ApplicationException was being done away with in
..NET 2.0. What do they suggest deriving from in 2.0? System.Exception?

Thanks so much.

Shawn
 
<"=?Utf-8?B?U2hhd24gQmVyZw==?=" <Shawn
Thanks for the reply. I have thought about using InvalidArgumentException,
but did not think it was right for this situation seeing that the argument
passed is valid in itself, there is just no data that it is associated with.
I guess it could go both ways, but that's how I interpreted it.

Where can I see a list of all of these pre-built exception classes that I
can choose from?

Well, if you go to System.Exception in MSDN, you can click on the
"Derived classes" link to get immediately derived classes, then look at
(say) System.SystemException and the classes that derive from that.
Also, I was not aware that ApplicationException was being done away with in
.NET 2.0.

It's not being "done away with" as such - it's just being deprecated.
What do they suggest deriving from in 2.0? System.Exception?

Yup.
 
Note that ApplicationException is deprecated in .NET 2.0 - MS realised
that it basically doesn't add anything, it just makes for a deeper
hierarchy.

Where did you get this information? I cannot find anything about
ApplicationException beeing deprecated.
I know that ApplicationException is not considered a good practice anymore
but is is really officially deprecated? At least VS 2005 generates no
warnings.
 
cody said:
Where did you get this information? I cannot find anything about
ApplicationException beeing deprecated.
I know that ApplicationException is not considered a good practice anymore
but is is really officially deprecated? At least VS 2005 generates no
warnings.

Hmmm - you're right, it's not coming up as deprecated in MSDN2 either.

I suspect they may not want to deprecate it "properly" due to the
number of warnings it would generate in perfectly valid existing code.
I think it should still be thought of as deprecated for new code
though.
 
Brad Abrams blogged about it...you should be able to google his site to find
the reference
 
Shawn Berg said:
After browsing the exceptions on MSDN I am unable to find an
"InvalidArgumentException".

I have found the following, but it's not listed there:

Sorry, yes, ArgumentException is the one I meant. I looked up
InvalidArgumentException in the MSDN index, saw that there was one
(it's for SharePoint) but didn't check the details :(
 
Back
Top