472,982 Members | 2,381 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,982 software developers and data experts.

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
8 3207
<"=?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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Ken Allen | last post by:
I have a .net client/server application using remoting, and I cannot get the custom exception class to pass from the server to the client. The custom exception is derived from ApplicationException...
15
by: bill salkin | last post by:
I'd like to create a custom error handler like this in VB.NET: .... try ... Throw ("Lender Name not in table") .... catch ("Lender Name not in table")
6
by: Steve Amey | last post by:
Hi all I want to be able to throw a custom error up the call stack. I have looked around and it seems as though it's possible, but I can't get it to work :o( Below is some sample code. ...
2
by: John | last post by:
Hi I was working fine with create user wizard and the default membership provider. I have now customised the membership provider as per attached web.config. The create user wizard picks up the...
1
by: David Herbst | last post by:
Enterprise Library Jan 2006 with Visual Studio 2005 on Windows 2000 Server sp4. My custom exception formatter fails with a "Unable to handle exception: 'LoggingExceptionHandler'." exception. ...
3
by: Dan | last post by:
Hi all! When I throw my custom Exception class the first time in my code, the compiler takes a lot of time for find the following catch EX: try Throw New MyCustomException("test")
1
by: problemKing | last post by:
Hi all, I had created a Remoting Custom Exception that inherits from System.Exception. Now, i meet the same problem that has been posted by previous thread...
3
by: matko | last post by:
This is a long one, so I'll summarize: 1. What are your opinions on raising an exception within the constructor of a (custom) exception? 2. How do -you- validate arguments in your own...
3
by: =?Utf-8?B?Vk1J?= | last post by:
How can I catch an error and throw the exception message plus some custom text? For example, I'd like to do something like this: try { //generate error } catch { //throw ex + " my custom...
0
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
I have a very odd problem which I hope someone can help me with. I have written a very simple test program using remoting as an excercise in using a custom sink - just a simple server with a single...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.