Error Trapping In Vb
in many circumstances. For example, suppose you try to open a text file that the user has deleted. When a compiled program has an error like this, an error message isdisplayed and the program ends. Although you cannot predict and write code for every possible type of error, "File Not Found" errors are fairly easy to handle. If you do not write code towork around the error, you can at least provide a message that makes more sense before ending the program. The "On Error" Statement The most common way to handle error conditions is to use Visual Basic's"On Error" statement.The "On Error" statement interrupts the normal flow of your program when an error occurs and begins executing your error handling code. A typical use is as follows :
On Error Goto FileOpenError When this statement is executed, any errors that occur in subsequentstatements cause Visual Basic to stop normal line-by-line execution andjump to the statement labeled as "FileOpenError". Labeling Code Lines Line labels in Visual Basic are similar to the line numbers of early BASIC.In Visual Basic, line labels can include text if you want, but each label mustbe unique. They are followed by a colon (:), as in the following example : Private Sub Form_Load () On Error Goto FileOpenError Open "C:\SOMEFILE.TXT" For Unput As #1 Line Input #1, sData Exit Sub FileOpenError: MsgBox "There was a problem opening the file. Stop for coffee!" End End Sub In the preceding sample code, if the "Open" or "Line Input" statements causean error, the statements starting at the label "FileOpenError" are executed,causing the message to be displayed and ending the program. You should note a few points about the sample code. First, note the locationand style of the error handling routine. It is usually placed near the end of thesubroutine, with the label not indented to indicate a special secresources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers Retired content Samples We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second. .NET Development Articles and Overviews Upgrading to Microsoft .NET Upgrading to Microsoft .NET Error Handling in Visual Basic .NET Error Handling in Visual Basic .NET Error Handling in Visual Basic .NET ADO.NET for the ADO Programmer Building an N-Tier Application in .NET Calling a .NET Component from a http://www.afralisp.net/archive/vba/error.htm COM Component Calling COM Components from .NET Clients Common .NET Libraries for Developers Comparing System.Xml in Visual Studio .NET to Microsoft.XMLDOM in Visual Studio 6.0 Converting ASP to ASP.NET Creating Classes in Visual Basic .NET Creating Components in .NET Creating a Windows Form User Control Data Binding with Windows Forms and ADO.NET Designing a .NET Application Designing for Web or Desktop? Determining When to Use https://msdn.microsoft.com/en-us/library/ms973849.aspx Windows Installer Versus XCOPY Differences Between Visual Basic 6.0 and .NET Controls Distributed Transactions in Visual Basic .NET Error Handling in Visual Basic .NET Getting Started with Windows Forms Inheritance and Interfaces Inheritance from a Base Class in Microsoft .NET Interacting with Message Queues Introduction to ASP.NET and Web Forms Introduction to Visual Studio .NET Managing Versions of an Application Migrating from the SOAP Toolkit to Web Services Overloading Methods in Visual Basic .NET Performing Drag-and-Drop Operations Raising Events and Responding to Events Replacing API Calls with .NET Framework Classes Structuring a .NET Application For Easy Deployment Understanding and Using Assemblies and Namespaces in .NET Using ActiveX Controls with Windows Forms in Visual Studio .NET Using ADO.NET Using COM+ Services in .NET Using Web Services Instead of DCOM Variable and Method Scope in Microsoft .NET Working with MDI Applications and Creating Menus TOC Collapse the table of content Expand the table of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. Error Handling in Visual Basic .NET Ken Getz MCW Technologies February 2002 Summary: Discusses how error handling differs between Visual Basic .NET a
error-free program can run smoothly and efficiently, otherwise all sorts of problems occur such as program crash or system hang. Errors often occur due to incorrect input from the user. For example, the user might make http://www.vbtutor.net/vb2008/vb2008_lesson20.html the mistake of attempting to enter a text (string) to a box that is designed to handle only numeric values such as the weight of a person, the computer will not be able to perform arithmetic calculation for text therefore will create an error. These errors are known as synchronous errors. Therefore a good programmer should be more alert to the parts of program that could trigger errors and should write error trapping errors handling code to help the user in managing the errors. Writing errors handling code should be considered a good practice for Visual Basic programmers, so do try to finish a program fast by omitting the errors handling code. However, there should not be too many errors handling code in the program as it create problems for the programmer to maintain and troubleshoot the program later. Visual Basic 2008 has improved a error trapping in lot in built-in errors handling compared to Visual Basic 6. For example, when the user attempts to divide a number by zero, Vb2008 will not return an error message but gives the 'infinity' as the answer (although this is mathematically incorrect, because it should be undefined) 20.2 Using On Error GoTo Syantax Visual Basic 2008 still supports the VB6 errors handling syntax, that is the On Error GoTo program_label structure. Although it has a more advanced error handling method, we shall deal with that later.We shall now learn how to write errors handling code in VB2008. The syntax for errors handling is On Error GoTo program_label where program_label is the section of code that is designed by the programmer to handle the error committed by the user. Once an error is detected, the program will jump to the program_label section for error handling.
Example 20.1: Division by Zero In this example, we will deal with the error of entering non-numeric data into the textboxes that suppose to hold numeric values. The program_label here is error_hanldler. when the user enter a non-numeric values into the textboxes, the error message will display the the text"One of the entries is not a number! Try again!". If no error occur, it will display the correct