-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path1425.html
More file actions
118 lines (114 loc) · 4 KB
/
Copy path1425.html
File metadata and controls
118 lines (114 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<h1 id="try">TRY</h1>
<blockquote>
<p>TRY</p>
</blockquote>
<p>The TRY statement introduces a TRY/CATCH block. A try/catch block
consist of the following structure:</p>
<p><strong>TRY</strong></p>
<p>The TRY statement starts a block of commands which might create a
run-time error.</p>
<p><strong>CATCH [var | expr]</strong></p>
<p>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.</p>
<p>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.</p>
<p><strong>END TRY</strong></p>
<p>The END TRY statement marks the end of a TRY/CATCH block.</p>
<h3 id="example-1-opening-a-non-existing-file-for-reading">Example 1:
Opening a non-existing file for reading</h3>
<pre><code>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"</code></pre>
<h3 id="example-2-open-com-port">Example 2: Open COM-Port</h3>
<pre><code>try
open "com2000:" AS #1
catch err
print "open failed: ";err
end try</code></pre>
<h3 id="example-3-using-error-expressions">Example 3: Using error
expressions</h3>
<pre><code>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"</code></pre>
<h3 id="example-4-advanced-error-handling-for-opening-files">Example 4:
Advanced error handling for opening files</h3>
<pre><code>' 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
opens = fn ' file opened, return file-handle (integer 1 to 256)
Catch err
? err; " ";
opens = 0 ' cannot open file, return 0 (FALSE)
End Try
End Func
' helper for demo:
Func demo(demo_number, open_mode)
Local fn
Color 14
?: ? Using " Demo #: "; demo_number;
Color 7
fn = opens(FILE_NAME, open_mode) ' Open file safely
If fn Then ? "File-handle is: "; fn; " ";
demo = fn ' return file-handle or 0 if error.
End Func
Kill FILE_NAME ' delete file before demo
fn = demo(1, "INPUT")
If fn Then Close #fn
fn = demo(2, "OUTPUT")
If fn Then
? #fn, "Demo 2 Works!"
Close #fn
Fi
fn = demo(3, "APPEND")
If fn Then
? #fn, "Demo 3 Works!"
Close #fn
Fi
fn = demo(4, "APPEND")
If fn Then
lines = ["Demo 4 Works!"]
Tsave #fn, lines
Close #fn
Fi
fn = demo(5, "INPUT")
If fn Then
Tload #fn, lines
? lines;
Close #fn
Fi
fn = demo(6, "INPUTX")
If fn Then
Tload #fn, lines
? lines;
Close #fn
Fi</code></pre>