====== CATCH ======
/* Remove the comments after creating content for each section.*/
Marks the beginning of a code block to be executed if an exception happens in the main portion of a subroutine
/*
For the lines below, just create links, separated by commas, to other pages of the BASICs section,
like [[:basics:basicTagName]].
"Implemented by" should mention BASICs which implement the keyword like described, in a more usual and standard way.
"With variations" should point to BASICs where this keyword is used a bit differently.
"Also written as" should list alternative spellings and abbreviations this keyword has. No need to
refer to a BASIC, there is a section for that later.
*/
**Implemented by:** [[:basics:Gambas:]]
**With variations:**
**Also written as: **
===== Usage =====
/* For inline examples of syntax, write the keyword between '' and '' (two single quotes) */
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
/*
==== Variations ====
===== Examples =====
*/
/*
Below is an example of a code block, using GeSHi syntax highlighting for //gwbasic//. Other styles of interest might be
//basic4gl//, //blitzbasic//, //freebasic//, //gambas//, //locobasic//, //purebasic//, //qbasic//, //sdlbasic//,
//thinbasic//, //vb//, //vbnet//, //xbasic// or //zxbasic//.
10 PRINT "HELLO"
*/
===== Comments =====
/* Origin of the keyword, opinions, history... */
At the time of this writing, CATCH seemed to be an oddity of [[:basics:gambas|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 =====
/* Keywords that are often or always used along this one */
* [[FINALLY]]
===== Similar keywords =====
/* Keywords with the same or similar functionality found in other versions of BASIC */
* [[TRY]]
* [[ERROR]]
* [[ON_ERROR|ON ERROR]]
===== In other languages... =====
/* For parallels and comparisons with other modern-day 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 =====
/* If you used the ((citation)) syntax elsewhere in the text, they will appear under this section. But you can write some recommendations of books, magazines, etc */
* https://gambaswiki.org/wiki/lang/catch , last check 2024-03-06
* https://gambaswiki.org/wiki/cat/error , last check 2024-03-06
/*
===== TAGS =====
Please use the syntax below for adding tags to this page, separated by spaces (use quotes for tags with spaces).
The most important tags to be used are, separated by groups:
- Statements | Commands | SpecialVariables | Operators
{{tag>tagName}}
*/
{{tag>Statements "Error Handling"}}