9 - 3 9 - 3
MELSEC-Q
9 THE CONCEPT OF ERROR HANDLING
9.3 Precautions Regarding Error Handling
Some precautions when creating an error handling routine are given below.
(1) Example of error prevention in a program
The error handling routine is the last defense against the program stopping. It is
better to create the program so that errors can be prevented in the program itself,
if possible, without using an error handling routine.
Example
10 INPUT " A=" ;A
20 INPUT "B=" ;B
30 C=A/B
40 PRINT " A/B =" ;C
If B is set to 0 in this program,
a Division by 0 error will occur
at line 30.
10 INPUT " A=" ;A
20 INPUT "B=" ;B
25 IF B=0 GOTO 20
30 C=A/B
40 PRINT " A=" ;C
Add line 25 so that line 30 will
not be executed if B = 0.
(2) Corrective action when an error that cannot be handled occurs
In the error handling routine, combine the ERR function and ERL function in an
appropriate manner and always make sure that generated error can be properly
handled in the error handling routine. If this is not checked, the error may not be
properly handled in the error handling routine and an error may occur in system
operation.
It is recommend to interrupt the error handling using “ON ERROR GOTO 0” and
stop the program operation if an error that cannot be handled occurs.
Example
Perform the error handling for preventing a wrong memory card from being
inserted. The program is stopped if any other errors occur.
10 ON ERROR GOTO 1000
100 OPEN " 0:DATA\110.DA T" FOR INPUT AS #1
200 OPEN " 0:DATA\120.DAT" FOR INPUT AS #2
1010 IF ERL=100 THEN PRINT "Insert the memory card that contains 110.DAT" :GOTO 1040 • • • •
1020 IF ERL=200 THEN PRINT "Insert the memory card that contains 120.DAT" :GOTO 1040 • • • •
• • • •
• • • • •
• • •
It the error is not File not Found, stop the program.
Check whether
the error occurred
at line 100 or line
200.
1030 ON ERROR GOTO 0 • • • • • •
If the error did not occur at line 100 or line 200, stop the program.
1040 PRINT "Press any key when the card is inserted"
1050 K$=INPUT$(1)
1060 RESUME • • • • • • Start the execution again from the instruction that generated the error.
900 END
11000 IF ERR<>53 THEN ON ERROR GOTO 0 • • •
Check if the error is File not Found.
If the error occurred at
line 100 or line 200,
an error handling
appropriate to each
case is performed.
• • •
• • •