'On [Local] Error Goto' and 'Resume [Next]' statements

Forum for discussion about the documentation project.
fxm
Posts: 9695
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

'On [Local] Error Goto' and 'Resume [Next]' statements

Postby fxm » Aug 07, 2011 18:31

Referring to this topic:
http://www.freebasic.net/forum/viewtopic.php?t=17527


The documentation is not very clear and even wrong on the legality and behavior of statements 'On [Local] Error Goto', 'Resume [Next]' regarding the compiler language (-lang) and options (none, -e, -ex, -exx):

- Seeing the first sentence of the paragraph 'Dialect Differences' of the documentation about the statements 'On Error' and 'Resume'/'Resume Next':
    Dialect Differences
      - Statement 'On Error' : ... ON ERROR is supported in the -lang qb dialect only.
      (I would have written : ON ERROR is supported in any -lang dialect)
      - Statement 'Resume [Next]' : ... ON ERROR is not supported in the -lang fb dialect.
      (I would have written : RESUME [NEXT] is not supported in the -lang fb dialect)
- There is no detail on the behavior of the statement 'On [Local] Error Goto' when no error checking option is required at compilation time:
=> inactive but consuming CPU time which can be masked by means of.....

- There is no detail on the behavior of the statement 'On [Local] Error Goto' when FB function is called using explicitly the returned error code:
=> ineffective and consuming CPU time, it is useless.

- Miscellaneous.


The behavior of statement 'On Error Goto' (-lang fb) regarding compilation options (none, -e/-ex/-exx) is enlighten with this following program including several examples (4), to be compiled with or without error checking option (4*2=8 tests):

Code: Select all

#DEFINE Config 1
'#DEFINE Config 2
'#DEFINE Config 3
'#DEFINE Config 4



#IF Config = 1 '-----------------------------------------------------------

Open "does_not_exist" For Input As #1

Print "main end"
Sleep
System

' - with compiler option 'none' :
'     console output :
'       'main end'
'
' - with compiler option '-e' or '-ex' or '-exx' :
'     console output :
'       'Aborting due to runtime error 2 (file not found) at line 10 of ...'

#ENDIF '-------------------------------------------------------------------



#IF Config = 2 '-----------------------------------------------------------

Dim As Integer Result = Open("does_not_exist" For Input As #1)
If Result <> 0 Then
  Print "file not found (processed by 'Result = Open(...)')"
End If

Print "main end"
Sleep
End

' - with compiler option 'none' or '-e' or '-ex' or '-exx' :
'     console output :
'       'file not found (processed by 'Result = Open(...)')'
'       'main end'

#ENDIF '-------------------------------------------------------------------



#IF Config = 3 '-----------------------------------------------------------

On Error Goto Error_Handler
Open "does_not_exist" For Input As #1

Print "main end"
Sleep
End

error_handler:
Print "file not found (processed by 'On Error Goto')"
On Error Goto 0
Print "QB-like error handling end"
Sleep
End

' - with compiler option 'none' :
'     console output :
'       'main end'
'
' - with compiler option '-e' or '-ex' or '-exx' :
'     console output :
'       'file not found (processed by 'On Error Goto')'
'       'QB-like error handling end'

#ENDIF '-------------------------------------------------------------------



#IF Config = 4 '-----------------------------------------------------------

On Error Goto error_handler
Dim As Integer Result = Open("does_not_exist" For Input As #1)
If Result <> 0 Then
  Print "file not found (processed by 'Result = Open(...)')"
End If

Print "main end"
Sleep
End

error_handler:
Print "file not found (processed by 'On Error Goto')"
On Error Goto 0
Print "QB-like error handling end"
Sleep
End

' - with compiler option 'none' or '-e' or '-ex' or '-exx' :
'     console output :
'       'file not found (processed by 'Result = Open(...)')'
'       'main end'

#ENDIF '-------------------------------------------------------------------


I'll update the documentation, while remaining concise as it is now.


[Edit]
- Wording.
Last edited by fxm on Aug 09, 2011 19:38, edited 1 time in total.
fxm
Posts: 9695
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: 'On [Local] Error Goto' and 'Resume [Next]' statements

Postby fxm » Aug 08, 2011 22:38

fxm wrote:I'll update the documentation, while remaining concise as it is now.

Done.

Impacted pages:
- CompilerDialects
- CompilerFeatures
- KeyPgOnerror
- KeyPgResume
- KeyPgResumenext
- ProPgErrorHandling
counting_pine
Site Admin
Posts: 6209
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Aug 09, 2011 8:43

Thanks for all your work here fxm. (Complex error handling gives me a headache.)

It looks like Config=2/4 tests the open() function. Hopefully there weren't any circumstances where you could persuade it to throw an error?
fxm
Posts: 9695
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Postby fxm » Aug 09, 2011 16:20

counting_pine wrote:It looks like Config=2/4 tests the open() function. Hopefully there weren't any circumstances where you could persuade it to throw an error?

Can you please rephrase your comment, I do not understand the exact meaning (probably due to my poor English).
counting_pine
Site Admin
Posts: 6209
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Aug 09, 2011 21:37

Sure thing, hope this is clearer:

It looks like in the Config=2 and Config=4 sections, you're testing the Open() feature, i.e. Open as a function returning an error value.

With this method of usage, any errors should just return a nonzero value without causing the program to abort or an ON ERROR routine to be called.

So the point of this post is just to confirm that Open() always behaves like that in your tests, regardless of any command line parameters or On Error statements.
fxm
Posts: 9695
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Postby fxm » Aug 09, 2011 22:02

Yes, it is just to demonstrate that when FB function is called using explicitly the returned error code (-lang fb dialect), ON ERROR is ineffective whatever the error checking level (none, -e, -ex, -exx).
fxm
Posts: 9695
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: 'On [Local] Error Goto' and 'Resume [Next]' statements

Postby fxm » Apr 14, 2012 8:55

Use of the 'Local' clause in the 'On [Local] Error Goto' statement (same behavior with option '-lang qb' or '-lang fb'):
  • Inside Sub/Function, the 'Local' clause seems to be ignored (inconsistent with the documentation):
    - the error handler 'label' can be either in the scope of the 'On [Local] Error Goto label' (using or not using the 'Local' clause),
    - or in the main module, but to be defined before the line 'On [Local] Error Goto label' of the Sub/Function (due to the single pass compiler?).
  • Inside main module, the 'Local' clause is forbidden (OK).

Code: Select all

Declare Sub test
test
End

Sub test
  On Error Goto label ' instead of 'On Local Error Goto label' which obviously works also
  Error 0
  Exit Sub
label:
  Print "Local procedure error handler"
  Sleep
  End
End Sub
Compilation successful and working fine!!!

Code: Select all

Declare Sub test
test
End

label:
  Print "Main module error handler"
  Sleep
  End

Sub test
  On Local Error Goto label ' instead of 'On Error Goto label' which obviously works also
  Error 0
End Sub
Compilation successful and working fine!!!


If it is intentional, or now at least desirable:
- the 'Local' clause is authorized only inside Sub/Function, but just for compatibility with PDS 7.1 and VB Dos 1.0, and it is ignored by the FreeBASIC compiler (error handler 'label' can be, either in the scope of the 'On [Local] Error Goto label', or in the main module),
I can modify the documentation.

If it is a bug, and now undesirable:
- exactly same behavior wanted as for PDS 7.1 and VB Dos 1.0,
I can fill a bug report.
fxm
Posts: 9695
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: 'On [Local] Error Goto' and 'Resume [Next]' statements

Postby fxm » Apr 24, 2012 18:04

Having received no response to my last post above, I however added a remark in the documentation:

KeyPgOnerror
Remark: Presently, the Local clause is ignored by the compiler.

KeyPgLocal
Remark: Presently, the Local clause (authorized only inside Sub/Function) is ignored by the compiler, and the error handler can be either in the scope of the same procedure the On [Local] Error is in, or in the main part of the module (if defined before the procedure).
fxm
Posts: 9695
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: 'On [Local] Error Goto' and 'Resume [Next]' statements

Postby fxm » May 07, 2012 13:28

fxm wrote:Having received no response to my last post above, I however added a remark in the documentation:

KeyPgOnerror
Remark: Presently, the Local clause is ignored by the compiler.

KeyPgLocal
Remark: Presently, the Local clause (authorized only inside Sub/Function) is ignored by the compiler, and the error handler can be either in the scope of the same procedure the On [Local] Error is in, or in the main part of the module (if defined before the procedure).

In addition, bug report filled 'On Local Error Goto ...' does not work as specified

Return to “Documentation”

Who is online

Users browsing this forum: No registered users and 1 guest