Results 1 to 9 of 9

Thread: How do you Raise an Error?

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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:
    1. Property LastName() As String
    2.             Get
    3.                 LastName = m_sLastName
    4.             End Get
    5.             Set(ByVal Value As String)
    6.                 If Value = "MENDHAK" Then
    7. [b]'Err.Raise ???[/b]
    8.  
    9.                 Else
    10.                 m_sLastName = Value
    11.                 End If
    12.  
    13.             End Set
    14.         End Property

  2. #2

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    I did this:

    VB Code:
    1. Property LastName() As String
    2.             Get
    3.                 LastName = m_sLastName
    4.             End Get
    5.             Set(ByVal Value As String)
    6.                 If Value = "MENDHAK" Then
    7.                     Throw (New Exception("Invalid Last Name!"))
    8.  
    9.  
    10.                 End If
    11.                 m_sLastName = Value
    12.  
    13.             End Set
    14.         End Property

    And caught it like this:

    VB Code:
    1. Try
    2.             myEmployee.LastName = "MENDHAK"
    3.         Catch myErr As Exception
    4.             MessageBox.Show(myErr.Message)
    5.  
    6.         Finally
    7.         End Try

    And it worked.

    I'm assuming that's the right way.

    Right?


  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Try
    2.             Throw New ArgumentException("Invalid Last Name!")
    3.  
    4.         Catch exArg As ArgumentException
    5.             MsgBox("An incorrect argument was passed!")
    6.         Catch ex As Exception
    7.             MsgBox("An unknown error occurred!")
    8.         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.

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .

  5. #5

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    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.

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  7. #7
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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/

  8. #8
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    hmm so can someone explain, why is throwing exceptions resource consuming? I'm kind of confused

    also, mendhak, take a look at the WHEN clause that can be used with the catch statement:

    Catch ex as exception when [true]

    only catches the exception when the statement after WHEN is evaluated as true... I think C# doesnt have this one though
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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
  •  



Click Here to Expand Forum to Full Width