Foutafhandeling

From Apache OpenOffice Wiki
< NL‎ | Documentation‎ | BASIC Guide
Revision as of 12:23, 26 January 2013 by DiGro (Talk | contribs)

Jump to: navigation, search
Book.png


Correcte afhandeling van foutsituaties is een van de meest tijdrovende taken van programmeren. Apache OpenOffice BASIC verschaft een aantal gereedschappen om de foutafhandeling te vereenvoudigen.

De instructie On Error

De instructie On Error is de sleutel voor elke foutafhandeling:

Sub Test
  On Error Goto ErrorHandler
  ' ... voer een taak uit gedurende welke een fout zou kunnen optreden
  Exit Sub
  ErrorHandler: 
    ' ... individuele code voor foutafhandeling
End Sub

De regel On Error Goto ErrorHandler definieert hoe Apache OpenOffice BASIC doorgaat in het geval van een fout. De opdracht Goto ErrorHandler verzekert u er van dat Apache OpenOffice BASIC de huidige programmaregel verlaat en vervolgens de code ErrorHandler: uitvoert.

De opdracht Resume

De opdracht Resume Next vervolgt het programma vanaf de regel die volgt op die waarin de fout voorkwam in het programma nadat de code in de foutafhandeling is uitgevoerd:

ErrorHandler:
  ' ... individuele code voor foutafhandeling
  Resume Next

Gebruik de opdracht Resume Proceed om een springpunt te specificeren om het programma door te laten gaan na de foutafhandeling:

ErrorHandler:
  ' ... individuele code voor foutafhandeling
  Resume Proceed
 
Proceed:
  ' ... het programma gaat hier door na de fout

De term Proceed is een label. Het zou bijvoorbeeld A247 kunnen zijn. De syntaxis voor namen van labels is hetzelfde als voor namen van variabelen.

Gebruik de volgende indeling om met een programma door te gaan zonder een foutbericht als er een fout optreedt:

Sub Test
  On Error Resume Next
  ' ... voer een taak uit gedurende welke een fout zou kunnen optreden
End Sub

Gebruik de opdracht On Error Resume Next voorzichtig want het effect ervan is global.

Query's met betrekking tot informatie over fouten

In foutafhandeling is het handig om een beschrijving van de fout te hebben en te weten waar en waarom die fout optrad:

  • De variabele Err bevat een getal dat het type fout identificeert dat optrad.
  • De variabele Error$ bevat een beschrijving van de fout.
  • De variabele Erl bevat het regelnummer waar de fout optrad.

De aanroep

 MsgBox "Fout " & Err & ": " & Error$ & " (regel : " & Erl & ")"

geeft weer hoe de informatie over de fout kan worden weergegeven in een berichtenvenster.

Documentation note.png VBA : Waar VBA de foutberichten samenvat in een global object genaamd Err, verschaft Apache OpenOffice Basic de variabelen Err, Error$ en Erl.


De statusinformatie blijft geldig totdat het programma een opdracht Resume of On Error tegenkomt, waarop de informatie opnieuw wordt ingesteld.

Documentation note.png VBA : In VBA stelt de methode Err.Clear van het object Err de status van een fout opnieuw in nadat er een fout optrad.

In Apache OpenOffice Basic wordt dit gedaan met de opdrachten On Error of Resume.


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