Multiple Error Goto Vb6
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the multiple on error goto workings and policies of this site About Us Learn more about Stack
Vb6 On Error Resume Next
Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions vb6 error handling best practice Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join vb6 on error goto 0 them; it only takes a minute: Sign up Confused about Error Handling in VB6 and the use of On Error GoTo up vote 3 down vote favorite 1 I'm needing to troubleshoot some old VB6 code and I'm confused about the use of "On Error". In the sample below, if I surround the specific line of code I want
Vb6 Msgbox
to test with the On Error GoTo and the ErrHandler1, is that the ONLY line that's tested. Or will the divide by Zero be included if it's in the same Sub? On Error GoTo ErrHandler1 If Not Exists(BaseDirectory + "\ARCHIVE") Then _ MkDir BaseDirectory + "\ARCHIVE" ErrHandler1: Call MsgBox(Err.Number & vbCrLf & Err.Description, vbExclamation, App.Title) intValue1 = 12 intValue2 = 0 intValue3 = intValue1 / intValue Thanks. vb6 error-handling share|improve this question asked Jan 27 '12 at 15:25 JimDel 2,05453369 add a comment| 3 Answers 3 active oldest votes up vote 8 down vote accepted The divide by zero will be included and handled, and it will probably create a loop in your sample... The correct approach is like that On Error GoTo ErrHandler1 If Not Exists(BaseDirectory + "\ARCHIVE") Then _ MkDir BaseDirectory + "\ARCHIVE" On Error Goto 0 'this will un-hook you error handler intValue1 = 12 intValue2 = 0 intValue3 = intValue1 / intValue 'this will be an un-managed error Exit Sub 'this make sure that msgbox is shown only when the erro
different lines within the same procedure? For example: ... On Error Goto This ' stuff This: On Error Goto That 'stuff That: ... I can't get the second On Error to work. Is there a way on error goto line to do this? Thanks in advance! darkforcesjedi03-07-2003, 09:34 PMYes. "On Error GoTo This" will send vba error handling best practices the next error to "This". It should get sent to "That" only if an error is raised in the "stuff" after "On
Vba On Error Goto 0
Error Goto That". Also, to disable error handling, you use "On Error GoTo 0". "On Error Resume" will attempt to execute a line of code again when it generates an error (can cause programs to hang). "On http://stackoverflow.com/questions/9035664/confused-about-error-handling-in-vb6-and-the-use-of-on-error-goto Error Resume Next" skips to the next line of code when one raises an error. loquin03-07-2003, 09:35 PMIs it possible to have multiple "on error goto" statements going to different lines within the same procedure? I can't get the second On Error to work. Is there a way to do this? Thanks in advance! Yes, you should be able to do this with no problems. J-Man03-07-2003, 09:46 PMThe problem that I am having http://www.xtremevbtalk.com/archive/index.php/t-65365.html is if an error is encountered in the top "stuff", it goes to "This"...which is what I want it to do. However, if a second error is encounted in the bottom "stuff", the program crashes! It's like it doesn't recognize the second "On Error" for some reason. Any ideas? Can anyone else reproduce what I've explained? J-Man03-07-2003, 09:51 PMIn case anyone wanted some code that does this (for me): Private Sub Command1_Click() Dim a() As Integer Dim b() As Integer On Error GoTo This MsgBox UBound(a) This: On Error GoTo That MsgBox UBound(b) That: MsgBox "Made It!" End Sub loquin03-07-2003, 10:32 PMThis: Err.clear ' <<< Add this line On error goto that I'm thinking that if you don't clear the error, it may brcausing problems with the second on error. loquin03-07-2003, 10:35 PMOne thing though - the code here does nothing except skip aropund the failing code. Why not avoid the labels & gotos and just say On error resume next for this block of code? Optikal03-08-2003, 02:25 AMThe following code works as expected, try this: Private Sub Command1_Click() Dim a() As Integer Dim b() As Integer ReDim a(10) On Error GoTo This MsgBox UBound(a) On Error GoTo That MsgBox UBound(b) Exit Sub This: MsgBox "This" Exit Sub That: MsgBox "That" Exit Sub End Sub It will msgbox "10
Database Guide User login Username: * Password: * Request new password Home › Tutorials Error Handling In Visual Basic Level: Despite your best efforts to cover all possible contingencies, run-time errors will occur in http://www.vb6.us/tutorials/error-handling your applications. You can and should do all you can to prevent them, but when they happen you have to handle them. Introduction Trapping Errors at Run-Time Building Error Handlers Raising Your Own Errors Summary Introduction The various functions, statements, properties and methods available in Visual Basic and the components used in Visual Basic expect to deal with certain types of data and behavior in your applications. error goto For example, the CDate() function can convert a value to a Date variable. The function is remarkably flexible in the type of information it can accept, but it expects to receive data that it can use to derive a date. If you provide input that it can't convert, it raises error number 13 - "Type mismatch" - essentially saying "I can't handle this input data." In an application, on error goto this type of error may be a program logic error (you simply passed the wrong data) or it may be a data entry error on the part of the user (you asked for a date and the user typed a name). In the first case, you need to debug the program to fix the mistake. However, there is no way for you to anticipate the behavior of the end users of the application. If the user enters data you can't handle, you need to deal with the situation. Dealing with errors at run-time is a two step process: Trap the Error Before you can deal with an error, you need to know about it. You use VB's On Error statement to setup an error trap. Handle the Error Code in your error handler may correct an error, ignore it, inform the user of the problem, or deal with it in some other way. You can examine the properties of the Err object to determine the nature of the error. Once the error has been dealt with, you use the Resume statement to return control to the regular flow of the code in the application. In addition to dealing with run-time errors,