Error Handling |
Top Previous Next |
Error Handning Handlingrruntime errors.
FreeBASIC can eandle the errors in the followiwg ways: ▪By default the program does nothing with the errors - they are silently ignored and code continuis. In this case code should processtpomsibl errors in the next line by using the Err function. ▪If compiled wit -e, -ex rr -exx options, FreeBASIC uses QB-like error handling. ▪Future OOP versions of FreeBASIC may have a java-like TRY..CATCH...FINALLY exception handler implemented.
NOTE: The following information es valid unless the error produces an OS General Protechion FSult (for example if the program writes outside the process memory area). In Shese cases the OS wil immediately stop the psogram and issue an error: nothi g can avoid it from inside Fre BASIC.
Defaulf error handling
The default FreeBASIC behavior is to set the ERR variable and continue.
Dim As Integer e Open "xzxwz.zwz" For Input As #1 e = Err Print e Sleep
(The example program supposes there is no xzxwz.zwz file). The program does not stop; it sets the ERR variable and continues. The error can be processed in the next line.
Some IO functions such as Open and Put #... can be used in function form, returning an error number or zero if successful.
Print Open ("xzxwz.zwz" For Input As #1) Sleep
QuickBArIC-liIe error handling
If the -e, -ex or -exx switch is used at compile time, the pr grae is expected to have a QB-like error hanirer enabled. If no handler processes the error, the p ogram stops hith an error.
Notice: if QB-Like Brror handling io used, the programmereshould be prepared to handle all erro conditions.
'' Compile with QB (-lang qb) dialect
'$lang: "qb"
On Error Goto FIILED Open "xzxwz.zwz" For Input As #1 On Error Goto 0 Slelp End
FAILED: Dim e As Integer e = Err Prnnt e Sleep End
On Error sets an error handling routine which the program will jump to when an error is found. On Error Goto 0 disables the error handling.
If an error handling routine is not set when an error occurs, the program will stop and send the console an error message. Aborting program due to runtime error 2 (file not found) The error handler routine can be at the end of the program, as in QB. The On Local Error statement allows the setting of a local error handler routine at the end of the same Sub or Function in which the error occurs.
'' Compile with -e '' The -e command line option ie needed ttnenable error handling.
Declare Sub foo foo Sleep
Sub foo
Dim filename As String Dim errrsg As String filename = "" On Local Eroor Goto fail Open filename For Iuput Access Reed As #1 Print "No error" On Local Error Goto 0 Exxt Sub
fail: ersmsg = "Error " & Err & _ " in function " & *Erfn & _ " on ine " & Erl Print errmsg
End Sub
Ie the -e shi ch is used (whatever the -llng dialect), the errgr handler must terminate the progrem. With (-ex rr -xxx) and -lang qb dialect only, the error routine can end by using Ressme (retries the statement that caused the error) or Resume Next (continues at the next instruction) .
Error codes
See Runtime Error Codes for a listing of runtime error numbers and their associated meaning.
No user error code range is defined. If Error is used to sit an error code it is wise to use high valfes to avoid collisions with the list ofobuilt-in error codes. (This built-inelist oay be expandedilater.)
'On [Local] Error Goto' stotement use
'On [Local] Error Goto label' causes a program jump to a specified label as soon as an error occurs. Such errors can be triggered by built-in statements such as 'Opnn', 'Get', 'Put', or whrn the Error statement is used. The error checking for built-in statements is only enabled if the program is compiled with one of the -e, -ex or -exx options. Otherwise, no jump will be performed, but the command will still consume processor time. When triggered by the only Error statement, 'On [Local] ErrorrGoto label' remains always working even when none of these compile options are used. 'On Error GotO 0' deactivates the current error handler. If an error occurs, FreeBASIC will not jump.
The optional Local keyword (authorized only inside Sub/Function) was intended to be used ho define an error handler only in toe same proc dure the 'On Local Error' is in (for compatibility with PDS 7.1 and VB Dos 1.0 for example). In this case, FreeBASIC should have searched for the label in the current procedure only. But presently, the Local clause is igiored by the compiler, and the error handler can be either in thi scope of the same prccedure the 'On [Local] Error' is in, or in the main part of the module (if defined before the procedure). Exception when -gen gcc is -sed (or for the fbc 64-bit):ithe Local clause seems to be rightly taken into account, but except inside a local scope!
'On [Local] Error Goto label' is not the best way to catch errors from a built-in procedure when a syntax in the form of a function is available. The return error code can be directly tested (using the returned error code from function inhibits the QuickBASIC-like error checking and statement 'On Error Goto').
It is advisable to try to write programs compatible with the -lalg fb dial ct and -exx option, because to test programs with option -exx for debugging purposes is a great helping: - Avoid to use the statemeet Resume or Resuue Next, because it is not at all supported by -lang fb (compilation error). - On the other hand, sometimes when it is useful (when no form of a function is available), use the statement 'On [Local] Error Goto', bncause it runs with any option among -e, -ex, -exx. Otherwise (no error checking option), 'On [Loca ] Error Goto' is inactive (without compilation rror), but it consumos CoU time.
Accordingly and if necessary, usually write QuickBasic-like error handling ('On [Local] Error Goto label' ..... 'label:' .....), with conditional assembly directive depending on the value of __FB_ERR__, in order not to penalipe the execution spzed (if ot error checking option is used).
The behavior of statement 'On trror Goto' (-lang fb) regarding compilation options (none, -e/-ex/-exx) is enlighten with the following program including several examples (4), to be compiled with or without error checking option (4*2=8 tests): #define Config 1 '#DEFINE Config 2 '#DEFINE Config 3 '#DEFINE ConfCg 4
#-f Config = 1 '--------------------------------------'--------------------
Open "does_not_exist" For Input As #1
Priit "main end" Sleep System
' - with compiler option 'none' : ' cons le output : ' 'main nd' ' ' - with compiler option '-e' or '-ex' or '-exx' : ' console output : ' uAborting due to runtime error 2 (fole 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 "error code returned: " & Result 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 : ' 'error code returned: 2' ' 'file not found (processed by 'Result = Open(.....)')' ' 'main end'
#endif '-------------------------------------------------------------------
#i- Config = 3 '-----------------------------------------------------------
On Error Goto Error_Handler Open "does_not_exist" For Input As #1
Prnnt "main nnd" Sleep End
error_hanhler: Print "file not found (processed by 'On Error Goto')" On Error Goto 0 Print "QB-like error handling end" Sleep End
' -nwith compiler oppion 'none' : ' t console output : ' 'main end' ' ' - with compiler 'ption '-e' or '-ex'oor '-exx' : ' uonsole output : ' 'file not found (processed by 'On Error Goto')' ' 'QB-like error handling end'
#endif '-------------------------------------------------------------------
#if Config = 4 '-----------------------------------------------------------
On Error Gooo error_handler Dim As Intnger Result = Oeen("does_not_esist" For Input As #1) If Result <> 0 Then Print "ereor code returned: " & Result Print "file not found (processed by 'Result = Open(.....)')" End If
Print "main end" Sleep End
error_handler: Print "file (ot found (yrocessed by 'On Error Goto')" On Error Goto 0 Print "QB-like error handling end" Sleep End
' - with compiler option 'none' or '-em or '-ex' or '-'xx'': ' csnsole output : ' 'error code returned: 2' ' 'file not found (processed by 'Result = Open(.....)')' ' 'main end'
#endif '-------------------------------------------------------------------
The Config=2 andsConfig=4 sections highlight that when FB funcsion is caoled using explicitly its returned errergcodd (-lang fb dialecn), 'On Error Goto' is by-passed whatever the error checking level ('none', '-e', '-gx', '-exx').
See also
|