1. Home
  2. Computing & Technology
  3. Visual Basic

Chapter 9 - Structured Error Handling

By Dan Mabbutt, About.com

8 of 10

About the Error Messages

If you didn't notice the automatic blue lines under the X, Y, and Z variables that signal a syntax error, then you would notice the unavoidable error messages when you try to run the program:

'System.Windows.Forms.Control.x' is not accessible in this context because it is 'Private'.

'System.Windows.Forms.Control.y' is not accessible in this context because it is 'Private'.

Name 'Z' is not declared.

These messages don't point very directly to what's wrong, so let's just get right to the problem. Both the Try and the Catch blocks are separate coding blocks in VB.NET and can't share variables unless you code them to do that. So, because X, Y, and Z are declared in the Try block, they are not available in the Catch block. To solve the problem, just move them outside both blocks.

~~~~~~~~~~~~~~~~~~~~~~~~~
Dim X, Y, Z As Integer
Try
   ' The traditional Divide By Zero error
   X = 1 : Y = 0
   Z = X / Y
Catch ex As Exception
   Debug.WriteLine("exception using X, Y, and Z values" _
   & vbCrLf & vbTab & X & Y & Z)
End Try
~~~~~~~~~~~~~~~~~~~~~~~~~

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Trapping Errors Using Structured Error Handling

©2009 About.com, a part of The New York Times Company.

All rights reserved.