|
2 | 2 |
|
3 | 3 | > TRY |
4 | 4 |
|
| 5 | +The TRY statement introduces a TRY/CATCH block. A try/catch block consist of the following structure: |
| 6 | + |
5 | 7 | __TRY__ |
6 | 8 |
|
7 | | -The TRY statement introduces a TRY/CATCH block. |
| 9 | +The TRY statement starts a block of commands which might create a run-time error. |
8 | 10 |
|
9 | 11 | __CATCH [var | expr]__ |
10 | 12 |
|
11 | | -The CATCH statement is used to CATCH an run-time error. 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. |
12 | | - |
13 | | -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. |
| 13 | +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. |
14 | 14 |
|
15 | | -When using the expression mode, you can supply a succession of CATCH statements to handle various error messages separately. |
| 15 | +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. |
16 | 16 |
|
17 | 17 | __END TRY__ |
18 | 18 |
|
|
26 | 26 | ' DON'T use existing file for demo. |
27 | 27 | open "try demo.tmp" for input as #1 |
28 | 28 | catch err |
29 | | - print err; " " |
| 29 | + print err |
30 | 30 | ' Some error handling could be implemented here |
31 | 31 | ' i.e: if(err = "...") then ... |
32 | 32 | end try |
33 | 33 |
|
34 | 34 | print "This point is reach, even if opening the file was not possible" |
35 | 35 | ``` |
36 | 36 |
|
37 | | -### Example 2: Advanced error handling for opening files |
| 37 | +### Example 2: Open COM-Port |
| 38 | + |
| 39 | +``` |
| 40 | +try |
| 41 | + open "com2000:" AS #1 |
| 42 | +catch err |
| 43 | + print "open failed: ";err |
| 44 | +end try |
| 45 | +``` |
| 46 | +### Example 3: Using error expressions |
| 47 | + |
| 48 | +``` |
| 49 | +try |
| 50 | + ' DON'T use existing file for demo. |
| 51 | + open "demo.tmp" for input as #1 ' Replace "demo.tmp" by "?.tmp" |
| 52 | +catch "FS(2): NO SUCH FILE OR DIRECTORY" |
| 53 | + print "File not found" |
| 54 | + ' Some error handling could be implemented here |
| 55 | + goto aftertrycatch |
| 56 | +catch "FS(22): INVALID ARGUMENT" |
| 57 | + print "Filename not allowed" |
| 58 | + ' Some error handling could be implemented here |
| 59 | +end try |
| 60 | +
|
| 61 | +label aftertrycatch |
| 62 | +print "end of program" |
| 63 | +``` |
38 | 64 |
|
39 | | -~~~ |
| 65 | +### Example 4: Advanced error handling for opening files |
40 | 66 |
|
| 67 | +``` |
41 | 68 | ' See also: Home -- Articles -- TRY / CATCH |
42 | 69 | Const FILE_NAME = "try demo.tmp" ' -- DON'T use existing file for demo. |
43 | 70 | ' OPEN file or device safely: |
@@ -99,14 +126,6 @@ If fn Then |
99 | 126 | ? lines; |
100 | 127 | Close #fn |
101 | 128 | Fi |
102 | | -~~~ |
| 129 | +``` |
103 | 130 |
|
104 | | -### Example 3: Open COM-Port |
105 | 131 |
|
106 | | -``` |
107 | | -try |
108 | | - open "com2000:" AS #1 |
109 | | -catch err |
110 | | - ? "in catch: open failed";err |
111 | | -end try |
112 | | -``` |
|
0 commit comments