CATCH
Marks the beginning of a code block to be executed if an exception happens in the main portion of a subroutine
Implemented by: Gambas
With variations:
Also written as:
Usage
CATCH is used in the body of a subroutine, right before its END, for defining a portion of code that must be executed if an exception/error occurs somewhere in the subroutine out of a TRY block.
It is important to notice that a CATCH block in Gambas must be placed after an occasional FINALLY block in the subroutine. The following example is found in Gambas documentation:
SUB PrintFile(FileName AS STRING) DIM hFile AS File DIM sLig AS STRING hFile = OPEN FileName FOR READ WHILE NOT EOF(hFile) LINE INPUT #hFile, sLig PRINT sLig WEND FINALLY ' Always executed, even if a error is raised. Warning: FINALLY must come before CATCH! CLOSE #hFile CATCH ' Executed only if there is an error PRINT "Cannot print file "; FileName END
Comments
At the time of this writing, CATCH seemed to be an oddity of Gambas only — alas, in most BASICs error handling is not exactly an area of excellence.
The fact that a FINALLY block must precede a CATCH block is counter-intuitive, and not by fortune different of every other programming language that implements "try-catch-finally" error handling.
Related keywords
Similar keywords
In other languages...
Most languages that have a catch statement use it as part of a try block. Quite often, multiple catch blocks can be used for treating different types of errors or exceptions. Java and PHP are some examples.
Tcl had a catch command long before it had try blocks, but it works somehow like a traditional try.
References
- https://gambaswiki.org/wiki/lang/catch , last check 2024-03-06
- https://gambaswiki.org/wiki/cat/error , last check 2024-03-06