|
-
May 22nd, 2004, 02:13 AM
#1
How do you Raise an Error?
In a class I'm writing, if a bad value is sent to a property, I'd like to raise an error, so that when the class is being used, a Try...Catch...Finally block can work with it.
Would I use an Err.Raise statement in here:
VB Code:
Property LastName() As String
Get
LastName = m_sLastName
End Get
Set(ByVal Value As String)
If Value = "MENDHAK" Then
[b]'Err.Raise ???[/b]
Else
m_sLastName = Value
End If
End Set
End Property
-
May 22nd, 2004, 02:19 AM
#2
I did this:
VB Code:
Property LastName() As String
Get
LastName = m_sLastName
End Get
Set(ByVal Value As String)
If Value = "MENDHAK" Then
Throw (New Exception("Invalid Last Name!"))
End If
m_sLastName = Value
End Set
End Property
And caught it like this:
VB Code:
Try
myEmployee.LastName = "MENDHAK"
Catch myErr As Exception
MessageBox.Show(myErr.Message)
Finally
End Try
And it worked.
I'm assuming that's the right way.
Right?
-
May 22nd, 2004, 02:42 AM
#3
Yup that is it. You can also raise a more specific exception type or make your own. They don't do anything different really unless the person is filtering their Try Catch block:
VB Code:
Try
Throw New ArgumentException("Invalid Last Name!")
Catch exArg As ArgumentException
MsgBox("An incorrect argument was passed!")
Catch ex As Exception
MsgBox("An unknown error occurred!")
End Try
I also read in some MSDN doc that it is better to use ApplicationException instead of Exception in your own apps but I forget what the difference was.
-
May 22nd, 2004, 03:07 AM
#4
Sleep mode
Remember , throwing exception error is always resource intensive . So , don't do it unless you have to . Instead you can use your own custom error message in a messagebox .
-
May 22nd, 2004, 03:13 AM
#5
Originally posted by Pirate
Remember , throwing exception error is always resource intensive . So , don't do it unless you have to . Instead you can use your own custom error message in a messagebox .
I see. Thanks, both of you.
-
May 22nd, 2004, 10:26 AM
#6
Only the first exception is a hog since it has to initialize the Exception engine. Depending on what kind of component/object you are making a messagebox could be a bad idea. If you use a messagebox then that limits the objects use to a WinForms project where you are allowed to show UI. It also doesn't allow the consumer of the class to handle the error how it wants to. Otherwise your class could be consumed by a windows or web application or things like a service that sometimes can't show UI components.
-
May 22nd, 2004, 03:26 PM
#7
yay gay
Yeah, using a MessageBox is IMO the worst idea possible, because if you put it in anything that might not be used in conjunction with GUI then you just can't find/control whenever you have an error.
\m/  \m/
-
May 23rd, 2004, 11:24 PM
#8
-
May 24th, 2004, 11:34 PM
#9
The very first exception in an application must initialize the Exception engine which can be slow but after that it is very fast.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|