467,174 Members | 1,339 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,174 developers. It's quick & easy.

Custom Exception

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?
Jul 21 '05 #1
  • viewed: 2930
Share:
8 Replies
<"=?Utf-8?B?U2hhd24gQmVyZw==?=" <Shawn
Be**@discussions.microsoft.com>> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
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

"Jon Skeet [C# MVP]" wrote:
<"=?Utf-8?B?U2hhd24gQmVyZw==?=" <Shawn
Be**@discussions.microsoft.com>> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3
<"=?Utf-8?B?U2hhd24gQmVyZw==?=" <Shawn
Be**@discussions.microsoft.com>> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4
> 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.
Jul 21 '05 #5
cody <de********@gmx.de> wrote:
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.


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #6
Brad Abrams blogged about it...you should be able to google his site to find
the reference

"cody" <de********@gmx.de> wrote in message
news:eH**************@TK2MSFTNGP14.phx.gbl...
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.

Jul 21 '05 #7
After browsing the exceptions on MSDN I am unable to find an
"InvalidArgumentException".

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

http://msdn.microsoft.com/library/de...classtopic.asp

Shawn

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"=?Utf-8?B?U2hhd24gQmVyZw==?=" <Shawn
Be**@discussions.microsoft.com>> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #8
Shawn Berg <sh***@saeweb.com> wrote:
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 :(

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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Ken Allen | last post: by
15 posts views Thread by bill salkin | last post: by
6 posts views Thread by Steve Amey | last post: by
2 posts views Thread by John | last post: by
1 post views Thread by problemKing | last post: by
3 posts views Thread by =?Utf-8?B?Vk1J?= | last post: by
reply views Thread by =?Utf-8?B?RGF2ZQ==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.