To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
Article :: Building Dynamic Systems with Expressions in .NET
How Is XML Like An Interface?
Understanding Covariance and Contravariance
Print VS 2010 Keyboard Shortcut References in Letter (8.5x11in) and A4 (210×297mm) Sizes
Updated Productivity Power Tools



Go Back   VBForums > Visual Basic > Visual Basic .NET

Reply Post New Thread
 
Thread Tools Display Modes
Old Sep 4th, 2009, 07:38 PM   #1
tassa
Fanatic Member
 
Join Date: Oct 08
Location: Dominican Republic
Posts: 732
tassa  is on a distinguished road (30+)
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
tassa is offline   Reply With Quote
Old Sep 4th, 2009, 08:16 PM   #2
kebo
Frenzied Member
 
kebo's Avatar
 
Join Date: Apr 04
Location: Gardnerville,nv
Posts: 1,443
kebo has a spectacular aura about (100+)kebo has a spectacular aura about (100+)
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.                       Select Case MathOperation
  12.                           Case Divide
  13.                             'Divide exception
  14.                           Case Multiply
  15.                               'Multiply exception
  16.                           Case Tangent
  17.                               'Calculate exception
  18.                       End Select
  19.                       Return "Error"
  20.                   End Try
__________________
It's too hard to read code that is not wrapped in code tags, so I don't. Use code tags around your code segments


Thank you for your support.
-Bartyles & James (circa 1980)

And in knowing that you know nothing, that makes you the smartest of all.
- Socrates (469 BC - 399 BC)
________________________________________________________________
Last edited by kebo : Now. Reason: superfluous typo's
kebo is offline   Reply With Quote
Old Sep 4th, 2009, 08:18 PM   #3
tassa
Fanatic Member
 
Join Date: Oct 08
Location: Dominican Republic
Posts: 732
tassa  is on a distinguished road (30+)
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
tassa is offline   Reply With Quote
Old Sep 4th, 2009, 08:30 PM   #4
danasegarane
Learning .Net
 
danasegarane's Avatar
 
Join Date: Aug 04
Location: VBForums
Posts: 5,016
danasegarane is a glorious beacon of light (400+)danasegarane is a glorious beacon of light (400+)danasegarane is a glorious beacon of light (400+)danasegarane is a glorious beacon of light (400+)danasegarane is a glorious beacon of light (400+)danasegarane is a glorious beacon of light (400+)
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

If my post was helpful to you, then Rate it

www.techreceipe.com

Tier Architecture
danasegarane is offline   Reply With Quote
Old Sep 4th, 2009, 08:32 PM   #5
kebo
Frenzied Member
 
kebo's Avatar
 
Join Date: Apr 04
Location: Gardnerville,nv
Posts: 1,443
kebo has a spectacular aura about (100+)kebo has a spectacular aura about (100+)
Re: Error handling question... Where did it come from?

yea... that would certainly simplify things
__________________
It's too hard to read code that is not wrapped in code tags, so I don't. Use code tags around your code segments


Thank you for your support.
-Bartyles & James (circa 1980)

And in knowing that you know nothing, that makes you the smartest of all.
- Socrates (469 BC - 399 BC)
________________________________________________________________
Last edited by kebo : Now. Reason: superfluous typo's
kebo is offline   Reply With Quote
Old Sep 4th, 2009, 09:00 PM   #6
jmcilhinney
.NUT
 
jmcilhinney's Avatar
 
Join Date: May 05
Location: Sydney, Australia
Posts: 60,536
jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)
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.
__________________

2007, 2008, 2009, 2010

Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs
MSDN "How Do I?" Videos: VB | C#
VBForums Database Development FAQ
My CodeBank Submissions: VB | C# (ForumAccount has translated some of my VB submissions to C#)
My Blog: Defining and Raising Custom Events | Manipulating GDI+ Drawings | Using Parameters in ADO.NET
jmcilhinney is offline   Reply With Quote
Old Sep 5th, 2009, 08:55 AM   #7
tassa
Fanatic Member
 
Join Date: Oct 08
Location: Dominican Republic
Posts: 732
tassa  is on a distinguished road (30+)
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
tassa is offline   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Visual Basic .NET


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 07:57 PM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.