473,411 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 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 3224
<"=?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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.