|
-
Sep 4th, 2009, 07:38 PM
#1
Thread Starter
Fanatic Member
[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 .
Thanks in advance!
PS: That's just an example.
-
Sep 4th, 2009, 08:16 PM
#2
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
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
-
Sep 4th, 2009, 08:18 PM
#3
Thread Starter
Fanatic Member
Re: Error handling question... Where did it come from?
-
Sep 4th, 2009, 08:30 PM
#4
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
-
Sep 4th, 2009, 08:32 PM
#5
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
-
Sep 4th, 2009, 09:00 PM
#6
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.
-
Sep 5th, 2009, 08:55 AM
#7
Thread Starter
Fanatic Member
Re: Error handling question... Where did it come from?
Ok, thanks a lot guys .
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
|