Difference between revisions of "Documentation/BASIC Guide/Error Handling"

From Apache OpenOffice Wiki
Jump to: navigation, search
m
 
(16 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{DISPLAYTITLE:Error Handling}}
 
 
{{Documentation/BASICGuideTOC/v2
 
{{Documentation/BASICGuideTOC/v2
 
|ShowPrevNext=block
 
|ShowPrevNext=block
 
|ShowPrevPage=block
 
|ShowPrevPage=block
 
|PrevPage=Documentation/BASIC Guide/Procedures and Functions
 
|PrevPage=Documentation/BASIC Guide/Procedures and Functions
|NextPage=Documentation/BASIC Guide/Runtime Library
+
|NextPage=Documentation/BASIC Guide/Other Instructions
 
|lang=block
 
|lang=block
 
}}
 
}}
 
+
{{DISPLAYTITLE:Error Handling}}
Correct handling of error situations is one of the most time-consuming tasks of programming. {{OOo}} Basic provides a range of tools for simplifying error handling.
+
__NOTOC__
 +
Correct handling of error situations is one of the most time-consuming tasks of programming. {{AOo}} Basic provides a range of tools for simplifying error handling.
  
 
== The <tt>On Error</tt> Instruction ==
 
== The <tt>On Error</tt> Instruction ==
Line 14: Line 14:
 
The <tt>On Error</tt> instruction is the key to any error handling:
 
The <tt>On Error</tt> instruction is the key to any error handling:
  
Sub Test
+
<syntaxhighlight lang="oobas">
  On Error Goto ErrorHandler
+
Sub Test
  ' ... undertake task during which an error may occur
+
  On Error Goto ErrorHandler
  Exit Sub
+
  ' ... undertake task during which an error may occur
  ErrorHandler:  
+
  Exit Sub
    ' ... individual code for error handling
+
  ErrorHandler:  
End Sub
+
    ' ... individual code for error handling
 +
End Sub
 +
</syntaxhighlight>
  
The <tt>On Error Goto ErrorHandler</tt> line defines how {{OOo}} Basic proceeds in the event of an error. The <tt>Goto ErrorHandler</tt> ensures that {{OOo}} Basic exits the current program line and then executes the <tt>ErrorHandler:</tt> code.  
+
The <tt>On Error Goto ErrorHandler</tt> line defines how {{AOo}} Basic proceeds in the event of an error. The <tt>Goto ErrorHandler</tt> ensures that {{AOo}} Basic exits the current program line and then executes the <tt>ErrorHandler:</tt> code.  
  
 
== The <tt>Resume</tt> Command ==
 
== The <tt>Resume</tt> Command ==
Line 28: Line 30:
 
The <tt>Resume Next</tt> command continues the program from the line that follows where the error occurred in the program after the code in the error handler has been executed:
 
The <tt>Resume Next</tt> command continues the program from the line that follows where the error occurred in the program after the code in the error handler has been executed:
  
ErrorHandler:
+
<syntaxhighlight lang="oobas">
  ' ... individual code for error handling
+
ErrorHandler:
  Resume Next
+
  ' ... individual code for error handling
 +
  Resume Next
 +
</syntaxhighlight>
  
 
Use the <tt>Resume Proceed</tt> command to specify a jump point for continuing the program after error handling:
 
Use the <tt>Resume Proceed</tt> command to specify a jump point for continuing the program after error handling:
  
ErrorHandler:
+
<syntaxhighlight lang="oobas">
  ' ... individual code for error handling
+
ErrorHandler:
  Resume Proceed
+
  ' ... individual code for error handling
 +
  Resume Proceed
 +
 +
Proceed:
 +
  ' ... the program continues here after the error
 +
</syntaxhighlight>
 +
The term Proceed is a label. It could be for example, A247. The syntax for label names is the same as for variable names.
 
   
 
   
Proceed:
 
  ' ... the program continues here after the error
 
 
 
To continue a program without an error message when an error occurs, use the following format:  
 
To continue a program without an error message when an error occurs, use the following format:  
  
Sub Test
+
<syntaxhighlight lang="oobas">
  On Error Resume Next
+
Sub Test
  ' ... perform task during which an error may occur
+
  On Error Resume Next
End Sub
+
  ' ... perform task during which an error may occur
 +
End Sub
 +
</syntaxhighlight>
  
 
Use the <tt>On Error Resume Next</tt> command with caution as its effect is global.
 
Use the <tt>On Error Resume Next</tt> command with caution as its effect is global.
Line 54: Line 63:
 
In error handling, it is useful to have a description of the error and to know where and why the error occurred:
 
In error handling, it is useful to have a description of the error and to know where and why the error occurred:
  
* The <tt>Err</tt> variable contains the number of errors that has occurred.  
+
* The <tt>Err</tt> variable contains a number identifying the type of error that has occurred.
 
* The <tt>Error$</tt> variable contains a description of the error.
 
* The <tt>Error$</tt> variable contains a description of the error.
 
* The <tt>Erl</tt> variable contains the line number where the error occurred.  
 
* The <tt>Erl</tt> variable contains the line number where the error occurred.  
Line 60: Line 69:
 
The call
 
The call
  
 +
<syntaxhighlight lang="oobas">
 
  MsgBox "Error " & Err & ": " & Error$ & " (line : " & Erl & ")"
 
  MsgBox "Error " & Err & ": " & Error$ & " (line : " & Erl & ")"
 +
</syntaxhighlight>
  
 
shows how the error information can be displayed in a message window.  
 
shows how the error information can be displayed in a message window.  
  
{{Documentation/Note|Whereas VBA summarizes the error messages in a statistical object called <tt>Err</tt>, {{OOo}} Basic provides the <tt>Err,</tt> <tt>Error$</tt>, and <tt>Erl</tt> variables.}}
+
{{Documentation/VBAnote|Whereas VBA summarizes the error messages in a global object called <tt>Err</tt>, {{AOo}} Basic provides the <tt>Err,</tt> <tt>Error$</tt>, and <tt>Erl</tt> variables.}}
  
 
The status information remains valid until the program encounters a <tt>Resume</tt> or <tt>On Error</tt> command, whereupon the information is reset.  
 
The status information remains valid until the program encounters a <tt>Resume</tt> or <tt>On Error</tt> command, whereupon the information is reset.  
  
{{Documentation/Note|In VBA, the <tt>Err.Clear</tt> method of the <tt>Err</tt> object resets the error status after an error occurs. In {{OOo}} Basic, this is accomplished with the <tt>On Error</tt> or <tt>Resume</tt> commands. }}
+
{{Documentation/VBAnote|In VBA, the <tt>Err.Clear</tt> method of the <tt>Err</tt> object resets the error status after an error occurs. In {{AOo}} Basic, this is accomplished with the <tt>On Error</tt> or <tt>Resume</tt> commands. }}
  
 
== Tips for Structured Error Handling ==
 
== Tips for Structured Error Handling ==
Line 80: Line 91:
 
The best solution is to use only one approach for error handling within a program - keep error handling separate from the actual program code and do not jump back to the original code after the error occurs.
 
The best solution is to use only one approach for error handling within a program - keep error handling separate from the actual program code and do not jump back to the original code after the error occurs.
  
The following is an example of an error handling procedure:
+
The following code is an example of an error handling procedure:
  
Sub Example
+
<syntaxhighlight lang="oobas">
  ' Define error handler at the start of the function  
+
Sub Example
  On Error Goto ErrorHandler   
+
  ' Define error handler at the start of the function  
    ' ... Here is the actual program code
+
  On Error Goto ErrorHandler   
  On Error Goto 0          ' Deactivate error handling
+
    ' ... Here is the actual program code
    ' End of regular program implementation
+
  On Error Goto 0          ' Deactivate error handling
  Exit Sub
+
    ' End of regular program implementation
 +
  Exit Sub
  
  ' Start point of error handling
+
' Start point of error handling
  ErrorHandler:                 
+
ErrorHandler:                 
    ' Check whether error was expected
+
  ' Check whether error was expected
    If Err = ExpectedErrorNo Then   
+
    If Err = ExpectedErrorNo Then   
      ' ... Process error
+
      ' ... Process error
    Else
+
    Else
      ' ... Warning of unexpected error
+
      ' ... Warning of unexpected error
    End If
+
    End If
  On Error Goto 0            ' Deactivate error handling  
+
On Error Goto 0            ' Deactivate error handling  
End Sub
+
End Sub
 +
</syntaxhighlight>
  
 
This procedure begins with the definition of an error handler, followed by the actual program code. At the end of the program code, the error handling is deactivated by the <tt>On Error Goto 0</tt> call and the procedure implementation is ended by the <tt>Exit Sub</tt> command (not to be confused with <tt>End Sub</tt>).  
 
This procedure begins with the definition of an error handler, followed by the actual program code. At the end of the program code, the error handling is deactivated by the <tt>On Error Goto 0</tt> call and the procedure implementation is ended by the <tt>Exit Sub</tt> command (not to be confused with <tt>End Sub</tt>).  
Line 107: Line 120:
 
The <tt>On Error Goto 0</tt> call at the end of the code resets the status information of the error (the error code in the <tt>Err</tt> system variables) so that an error occurring at a later date can be clearly recognized.
 
The <tt>On Error Goto 0</tt> call at the end of the code resets the status information of the error (the error code in the <tt>Err</tt> system variables) so that an error occurring at a later date can be clearly recognized.
  
 
+
 +
{{InterWiki Languages BasicGuide|articletitle=Documentation/BASIC Guide/Error Handling}}
 
{{PDL1}}
 
{{PDL1}}

Latest revision as of 12:03, 30 January 2021


Correct handling of error situations is one of the most time-consuming tasks of programming. Apache OpenOffice Basic provides a range of tools for simplifying error handling.

The On Error Instruction

The On Error instruction is the key to any error handling:

Sub Test
  On Error Goto ErrorHandler
  ' ... undertake task during which an error may occur
  Exit Sub
  ErrorHandler: 
    ' ... individual code for error handling
End Sub

The On Error Goto ErrorHandler line defines how Apache OpenOffice Basic proceeds in the event of an error. The Goto ErrorHandler ensures that Apache OpenOffice Basic exits the current program line and then executes the ErrorHandler: code.

The Resume Command

The Resume Next command continues the program from the line that follows where the error occurred in the program after the code in the error handler has been executed:

ErrorHandler:
  ' ... individual code for error handling
  Resume Next

Use the Resume Proceed command to specify a jump point for continuing the program after error handling:

ErrorHandler:
  ' ... individual code for error handling
  Resume Proceed
 
Proceed:
  ' ... the program continues here after the error

The term Proceed is a label. It could be for example, A247. The syntax for label names is the same as for variable names.

To continue a program without an error message when an error occurs, use the following format:

Sub Test
  On Error Resume Next
  ' ... perform task during which an error may occur
End Sub

Use the On Error Resume Next command with caution as its effect is global.

Queries Regarding Error Information

In error handling, it is useful to have a description of the error and to know where and why the error occurred:

  • The Err variable contains a number identifying the type of error that has occurred.
  • The Error$ variable contains a description of the error.
  • The Erl variable contains the line number where the error occurred.

The call

 MsgBox "Error " & Err & ": " & Error$ & " (line : " & Erl & ")"

shows how the error information can be displayed in a message window.

Documentation note.png VBA : Whereas VBA summarizes the error messages in a global object called Err, Apache OpenOffice Basic provides the Err, Error$, and Erl variables.


The status information remains valid until the program encounters a Resume or On Error command, whereupon the information is reset.

Documentation note.png VBA : In VBA, the Err.Clear method of the Err object resets the error status after an error occurs. In Apache OpenOffice Basic, this is accomplished with the On Error or Resume commands.


Tips for Structured Error Handling

Both the definition command, On Error, and the return command, Resume, are variants of the Goto construct.

If you want to cleanly structure your code to prevent generating errors when you use this construct, you should not use jump commands without monitoring them.

Care should be taken when you use the On Error Resume Next command as this dismisses all open error messages.

The best solution is to use only one approach for error handling within a program - keep error handling separate from the actual program code and do not jump back to the original code after the error occurs.

The following code is an example of an error handling procedure:

Sub Example
  ' Define error handler at the start of the function 
  On Error Goto ErrorHandler   
    ' ... Here is the actual program code
  On Error Goto 0           ' Deactivate error handling
    ' End of regular program implementation
  Exit Sub
 
 ' Start point of error handling
 ErrorHandler:                
   ' Check whether error was expected
    If Err = ExpectedErrorNo Then   
      ' ... Process error
    Else
      ' ... Warning of unexpected error
    End If
 On Error Goto 0            ' Deactivate error handling 
End Sub

This procedure begins with the definition of an error handler, followed by the actual program code. At the end of the program code, the error handling is deactivated by the On Error Goto 0 call and the procedure implementation is ended by the Exit Sub command (not to be confused with End Sub).

The example first checks if the error number corresponds to the expected number (as stored in the imaginary ExpectedErrorNo constant) and then handles the error accordingly. If another error occurs, the system outputs a warning. It is important to check the error number so that unanticipated errors can be detected.

The On Error Goto 0 call at the end of the code resets the status information of the error (the error code in the Err system variables) so that an error occurring at a later date can be clearly recognized.


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools