Error Trapping Visual Basic 6.0
Database Guide User login Username: * Password: * Request new password Home › Tutorials Error handling in Visual Basic Level: Error handling is essential to all professional applications. Any number of run-time errors can occur, and if your program does not trap them, the VB default action is to report the error and then terminate the program (often resulting in the end user calling you and complaining, "Your program kicked me out!"). By placing error-handling code in your program, you can trap a run-time error, report it, and let the user continue. Sometimes the user will be able to correct the error and sometimes not, but simply allowing the program to crash is not acceptable. You should generally place error-handling code in any Sub or Function that accesses files or databases. Your code will typically interrogate the Number and Description properties of the built-in VB Err object in an error-handling routine set up with the On Error statement. In this section, we will look at the following statements: On Error GoTo label On Error Resume Next Following is a brief tutorial in error-handling. To perform this tutorial, you should have a floppy disk handy. Also, in the VB IDE, make sure that the Break on Unhandled Errors option is set under Tools à Options à General. STEPS: 1. Start a new project. 2. Place four command buttons on the form. Name them and set their Captions as follows: Name Caption cmdCrash Crash cmdGoToLabel GoTo Label cmdGoTo0 GoTo 0 cmdResumeNext Resume Next Your form should look something like this: 3. Code the cmdCrash_Click event as follows: Private Sub cmdCrash_Click() Open "A:\JUNK.TXT" For Input As #1 MsgBox "File was opened successfully" Close #1 End Sub 4. Place your floppy disk in the A: drive. Run the program and click the Crash button. Assuming that you do not have a file called "JUNK.TXT" on your A: disk, the program will "bomb" with the code/message "53 – File Not Found". If you don't have a disk in drive A:, the code/message will be "71 – Disk Not Ready". 5. Code the cmdGoToLabel_Click event: Copy and paste the code from the Crash sub, and add statements so that the cmdGo
resources 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 COM Component Calling http://www.vb6.us/tutorials/error-handling-visual-basic 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 Windows Installer Versus XCOPY Differences https://msdn.microsoft.com/en-us/library/ms973849.aspx 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 and Visual Basic 6.0. Main topics include Try/Catch blocks, Exc
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring http://stackoverflow.com/questions/116289/what-is-the-better-way-to-handle-errors-in-vb6 developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up what is the better way to handle errors in VB6 up vote 12 down vote favorite 6 I have VB6 application , I want to put some good error handling finction in error trapping it which can tell me what was the error and exact place when it happened , can anyone suggest the good way to do this vb6 error-handling share|improve this question edited Sep 22 '08 at 17:36 Onorio Catenacci 9,01575386 asked Sep 22 '08 at 17:34 RBS 1,30192330 I removed the "in" tag. –Onorio Catenacci Sep 22 '08 at 17:36 I also changed the two separate "error" and "handling" tags to "error-handling" –Onorio Catenacci Sep 22 error trapping visual '08 at 17:37 add a comment| 7 Answers 7 active oldest votes up vote 2 down vote accepted ON ERROR GOTO and the Err object. See this discussion. share|improve this answer answered Sep 22 '08 at 17:42 Joe Skora 8,97142430 add a comment| up vote 27 down vote First of all, go get MZTools for Visual Basic 6, its free and invaluable. Second add a custom error handler on every function (yes, every function). The error handler we use looks something like this: On Error GoTo {PROCEDURE_NAME}_Error {PROCEDURE_BODY} On Error GoTo 0 Exit {PROCEDURE_TYPE} {PROCEDURE_NAME}_Error: LogError "Error " & Err.Number & " (" & Err.Description & ") in line " & Erl & _ ", in procedure {PROCEDURE_NAME} of {MODULE_TYPE} {MODULE_NAME}" Then create a LogError function that logs the error to disc. Next, before you release code add Line Numbers to every function (this is also built into MZTools). From now on you will know from the Error Logs everything that happens. If possible, also, upload the error logs and actually examine them live from the field. This is about the best you can do for unexpected global error handling in VB6 (one of its many defects), and really this should only be used to find unexpected errors. If you know that if there is the possibility of an error occurring in a certain situation, you should catch that particular error and handle for it.