[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:
Try
Select Case MathOperation
Case Divide
'Divide a/b
Case Multiply
'Multiply a*b
Case Tangent
'Calculate tangent
End Select
Catch ex As Exception
Return "Error"
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 :D.
Thanks in advance!
PS: That's just an example.
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:
Try
Select Case MathOperation
Case Divide
'Divide a/b
Case Multiply
'Multiply a*b
Case Tangent
'Calculate tangent
End Select
Catch ex As Exception
Select Case MathOperation
Case Divide
'Divide exception
Case Multiply
'Multiply exception
Case Tangent
'Calculate exception
End Select
Return "Error"
End Try
Re: Error handling question... Where did it come from?
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
Re: Error handling question... Where did it come from?
yea... that would certainly simplify things :thumb:
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.
Re: Error handling question... Where did it come from?
Ok, thanks a lot guys ;).