TRY
TRY
The TRY statement introduces a TRY/CATCH block. A try/catch block consist of the following structure:
TRY
The TRY statement starts a block of commands which might create a run-time error.
CATCH [var | expr]
The CATCH statement is used to catch a run-time error of one of the commands in the try-block. This is typically used with errors raised when calling a file system command that cannot be completed, for example attempting to open a non-existent file.
The CATCH statement has two modes. You can supply a variable argument to store the error string. Alternatively you can supply an expression. When the raised error matches the (String) expression, the error will be caught. When using the expression mode, you can supply a succession of CATCH statements to handle various error messages separately.
END TRY
The END TRY statement marks the end of a TRY/CATCH block.
Example 1: Opening a non-existing file for reading
try
' DON'T use existing file for demo.
open "try demo.tmp" for input as #1
catch err
print err
' Some error handling could be implemented here
' i.e: if(err = "...") then ...
end try
print "This point is reach, even if opening the file was not possible"
Example 2: Open COM-Port
try
open "com2000:" AS #1
catch err
print "open failed: ";err
end try
Example 3: Using error expressions
try
' DON'T use existing file for demo.
open "demo.tmp" for input as #1 ' Replace "demo.tmp" by "?.tmp"
catch "FS(2): NO SUCH FILE OR DIRECTORY"
print "File not found"
' Some error handling could be implemented here
goto aftertrycatch
catch "FS(22): INVALID ARGUMENT"
print "Filename not allowed"
' Some error handling could be implemented here
end try
label aftertrycatch
print "end of program"
Example 4: Advanced error handling for opening files
' See also: Home -- Articles -- TRY / CATCH
Const FILE_NAME = "try demo.tmp" ' -- DON'T use existing file for demo.
' OPEN file or device safely:
Func opens(filename, mode)
Local fn = Freefile
Try
Select Case Lcase(mode)
Case "input" : Open filename For Input As #fn
Case "output": Open filename For Output As #fn
Case "append": Open filename For Append As #fn
Case Else: ? "opens(): Bad open mode at line " + Progline: Stop
End Select
' file opened, return file-handle (integer 1 to 256)
opens = fn Catch err
" ";
? err; 0 ' cannot open file, return 0 (FALSE)
opens = End Try
End Func
' helper for demo:
Func demo(demo_number, open_mode)
Local fn
Color 14
" Demo #: "; demo_number;
?: ? Using Color 7
' Open file safely
fn = opens(FILE_NAME, open_mode) If fn Then ? "File-handle is: "; fn; " ";
' return file-handle or 0 if error.
demo = fn End Func
Kill FILE_NAME ' delete file before demo
1, "INPUT")
fn = demo(If fn Then Close #fn
2, "OUTPUT")
fn = demo(If fn Then
#fn, "Demo 2 Works!"
? Close #fn
Fi
3, "APPEND")
fn = demo(If fn Then
#fn, "Demo 3 Works!"
? Close #fn
Fi
4, "APPEND")
fn = demo(If fn Then
"Demo 4 Works!"]
lines = [Tsave #fn, lines
Close #fn
Fi
5, "INPUT")
fn = demo(If fn Then
Tload #fn, lines
? lines;Close #fn
Fi
6, "INPUTX")
fn = demo(If fn Then
Tload #fn, lines
? lines;Close #fn
Fi