Results 1 to 7 of 7

Thread: [RESOLVED] Error handling question... Where did it come from?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Resolved [RESOLVED] Error handling question... Where did it come from?

    Hello,

    I'm wondering if there is any way to know where did the error come from. I have this:

    vb.net Code:
    1. Try
    2.                 Select Case MathOperation
    3.                     Case Divide
    4.                         'Divide a/b
    5.                     Case Multiply
    6.                         'Multiply a*b
    7.                     Case Tangent
    8.                         'Calculate tangent
    9.                 End Select
    10.             Catch ex As Exception
    11.                 Return "Error"
    12.             End Try

    When I say "where did the error come from, is which case threw the error.I know I could check the values and throw an exception, but I want to know which case produced the exception.

    I hope it's clear .

    Thanks in advance!

    PS: That's just an example.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Error handling question... Where did it come from?

    You can get the line number through the exception's StackTrace property, but to get the actual case, you would probably need to do a second case select in the exception block


    vb Code:
    1. Try
    2.                       Select Case MathOperation
    3.                           Case Divide
    4.                               'Divide a/b
    5.                           Case Multiply
    6.                               'Multiply a*b
    7.                           Case Tangent
    8.                               'Calculate tangent
    9.                       End Select
    10.                   Catch ex As Exception
    11.  
    12.                       Select Case MathOperation
    13.                           Case Divide
    14.                             'Divide exception
    15.                           Case Multiply
    16.                               'Multiply exception
    17.                           Case Tangent
    18.                               'Calculate exception
    19.                       End Select
    20.  
    21.  
    22.                       Return "Error"
    23.                   End Try
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Error handling question... Where did it come from?

    Oh great!!! THANKS!!!
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  4. #4
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Error handling question... Where did it come from?

    You have to get the details from the Stack Trace Only.

    I won't go with the method suggested by kebo, though it is good. This will increase the complexity of the Program. As you have the MathOperation varriable in the Try block you can always check the text of this varriable and get the case which cause the Exception as

    Code:
    Dim MathOperation As String = "Divide"
            Try
                Select Case MathOperation
                    Case "Divide"
                        Dim x As Integer = Convert.ToInt16("D")
                    Case "Multiply"
                        'Multiply a*b
    
                        'Calculate tangent
                End Select
            Catch ex As Exception
                MessageBox.Show("Error came from " & MathOperation)
            End Try
    Please mark you thread resolved using the Thread Tools as shown

  5. #5
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Error handling question... Where did it come from?

    yea... that would certainly simplify things
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Error handling question... Where did it come from?

    Depending on what you actually intend to do in the Catch block, a separate exception handler for each Case might be the most appropriate option.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Error handling question... Where did it come from?

    Ok, thanks a lot guys .
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

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