|
-
Apr 16th, 2001, 02:44 AM
#1
Thread Starter
Lively Member
Look at this code:
Code:
On Error GoTo err_han
Dim DataFile as Field
Do Until recordset1.EOF
strPath = DataFile
Open strPath For Input As #1
lab1: recordset1.MoveNext
Loop
Exit Sub
err_han:
Select Case Err.Number
Case Is = 53
Label2.Caption = "No file to open"
DoEvents
GoTo lab1
Case Else
MsgBox Err.Number & " - " & Err.Description & " - " & Err.Source
End Select
On first execution of DO LOOP error handler traps err.number 53 (File ton found)- That OK!
On next execution (second step) error handler does not work and I get Run time error '53'. File not found!
Is this weird or what?
Ermin Gutenberger
VB.NET 2010
-
Apr 16th, 2001, 02:48 AM
#2
i know...
i had the same problem ages ago! it annoyed the crap outta me.
put an On Error goto err_han after the err_han: line
vb will produce error messages if an error occurs while inside an error handler subroutine! so u tell vb that u r not handling errors anymore, and if one happens, goto [label]. sort of like a trick
-
Apr 16th, 2001, 03:13 AM
#3
Thread Starter
Lively Member
If I tell VB not to handle errors it my code will create run time error very often. I use error handler to trap all kinds of errors and in error handler I have code that handles that error. This code can be quite complex.
So I can't tel VB not to handle error.
Ermin Gutenberger
VB.NET 2010
-
Apr 16th, 2001, 03:16 AM
#4
sorry bad explanation
sorry i am not good at explainin code, just givin it! hehe lol
just do what i told ya and it will work
-
Apr 16th, 2001, 04:12 AM
#5
Vb doesn't invoke the error handler if it thinks it is handling an error already. So an error inside the error handler will not be handled by this error handler.
This means you need to tell vb that error handling is finished, and you resume with normal code.
You do this with the Resume keyword.
Replace 'GoTo lab1' with 'Resume lab1' and all should work again.
-
Apr 16th, 2001, 04:45 AM
#6
took the words right out of my mouth
-
Apr 16th, 2001, 05:36 AM
#7
Well ...
Originally posted by Frans C
Vb doesn't invoke the error handler if it thinks it is handling an error already. So an error inside the error handler will not be handled by this error handler.
This means you need to tell vb that error handling is finished, and you resume with normal code.
You do this with the Resume keyword.
Replace 'GoTo lab1' with 'Resume lab1' and all should work again.
I tend to disagree with the former part of your opinion. I have some code in which I have created the error handler as a DLL. Every time there is an error, from my application I call the error object in my DLL. I had forgotten to create a new instance of this object in the first place. So the first time an error occured in my application, the control went inside the error handler, tried to invoke the error object in my DLL and since it had not been instantiated, fired another error, which was again trapped inside the same error handler.
So I think even if your error handler causes any errors, they will be trapped by the same error handling code.
And I agree with the later part, that you should use 'Resume ...' to resume control in the normal code.
.
-
Apr 16th, 2001, 05:55 AM
#8
If I understood it right, the following thing happens:
If an error occurs in a procedures error handler, the first error handler up in the call stack is invoked. If there is no such error handler, a standard error message is displayed and the program is terminated.
The following test proves this:
Code:
Private Sub Command1_Click()
Call CheckError1
End Sub
Private Sub CheckError1()
On Error GoTo HandleIt
Call CheckError2
Exit Sub
HandleIt:
MsgBox "CheckError1"
End Sub
Private Sub CheckError2()
Call CheckError3
End Sub
Private Sub CheckError3()
Dim x As Double
On Error GoTo HandleIt
x = 1 / 0
Exit Sub
HandleIt:
MsgBox "CheckError3"
x = 1 / 0
End Sub
The first error occurs in procedure CheckError3, the error handler is invoked, and the messagebox says "CheckError3". Next another error is generated inside the error handler. Because the error is invoked inside the error handler of CheckError3, its error handler is not invoked, instead the error handler of CheckError2 should be invoked, but this procedure doesn't have one. One step up in the call stack is procedure CheckError1, and this his an error handler. The next messagebox says CheckError1.
I rest my case.
-
Apr 16th, 2001, 07:59 AM
#9
Well ...
Yikes! I hate to say this, but
YOU ARE RIGHT!
Code:
Option Explicit
Private Sub Form_Activate()
On Error GoTo MyError
Err.Raise 5
Exit Sub
MyError:
MsgBox Err.Description
Err.Raise 13
End Sub
I shall seek solace in proving you right in fewer lines of code!
.
-
Apr 16th, 2001, 08:14 AM
#10
But your example doesn't show that the calling sub's error handler is used.
-
Apr 16th, 2001, 08:39 AM
#11
Thread Starter
Lively Member
Thanks guys. I solved my problem with
resume lab1
code. But then I saw that my code isn't written as it should be so I rewrite it and doesn't need error handling in that part anymore.
But it was a good school of error handling!
Thanks!
Ermin Gutenberger
VB.NET 2010
-
Apr 16th, 2001, 09:11 AM
#12
Well ...
Originally posted by Frans C
But your example doesn't show that the calling sub's error handler is used.
If you try the example, the runtime error 5 is trapped in the error handler MyError. Then the error 13 is not trapped, and VB pops up its error dialog. This proves that the same error handler is not used to handle errors inside the error handling code.
According to my argument, even for error 13, the MyError code should have been invoked. But since it does not happen so, my argument is false.
.
-
Apr 16th, 2001, 09:55 AM
#13
New Member
The error you are encountering is due to the fact you haven't closed the initial file you were trying to read.
you opened a file within the loop but did not CLOSE it. The second pass of the loop is tryin to open another file
to the same file handle. If you are trying to open multiple sources you will need to change the File handle to a variable that is different for each one.
[ORIGINAL]
On Error GoTo err_han
Dim DataFile as Field
Do Until recordset1.EOF
strPath = DataFile
Open strPath For Input As #1
lab1: recordset1.MoveNext
Loop
[Option #1]
On Error GoTo err_han
Dim DataFile as Field
Do Until recordset1.EOF
strPath = DataFile
Open strPath For Input As #1
Close (#1)
lab1: recordset1.MoveNext
Loop
[Option #2]
[ORIGINAL]
On Error GoTo err_han
Dim DataFile as Field
Do Until recordset1.EOF
strPath = DataFile
fhInFile = freefile
Open strPath For Input As #(fhInFile)
lab1: recordset1.MoveNext
Loop
If you are trying to determine if a file exists then try this instead.
On Error GoTo err_han
Dim DataFile as Field
Do Until recordset1.EOF
strPath = DataFile
If len(dir(strpath)) > 0 then
' this is a valid file.
else
' this file does NOT exist.
end if
lab1: recordset1.MoveNext
Loop
-
Apr 16th, 2001, 02:03 PM
#14
Thread Starter
Lively Member
Actually I forgot to write in my sample code
close #1.
A did that somewhere in the proccess, so my filenumber #1 was "free" for new open file.
but any way VB should trap error for wrong file number but it didn't.
That was NOT my problem.
solved my problem. But as I said I rewrote entire procedure so i don't need error handling anymore!
Ermin Gutenberger
VB.NET 2010
-
Apr 16th, 2001, 04:33 PM
#15
Addicted Member
Simple. If you wanna stop the file not found error, tell VB to open another file, a dummy file, and then close it. Then inform the user if it's their fault.
Why does everyone think I may be dangerous?  I'm just good at computers. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|