structured_20exception_20handling
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
structured_20exception_20handling [2018/03/31 13:19] – external edit 127.0.0.1 | structured_20exception_20handling [2024/01/05 00:21] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 2: | Line 2: | ||
//by Richard Russell, April 2008//\\ \\ Several modern programming languages (e.g. **C++**, **Java** and **Visual Basic**) provide a [[http:// | //by Richard Russell, April 2008//\\ \\ Several modern programming languages (e.g. **C++**, **Java** and **Visual Basic**) provide a [[http:// | ||
+ | <code java> | ||
try | try | ||
{ | { | ||
Line 10: | Line 11: | ||
// Code to execute if an error occurred in the ' | // Code to execute if an error occurred in the ' | ||
} | } | ||
- | The **try** clause contains the code you want to execute, which may consist of any number of statements and may optionally call subroutines. This is the code that you anticipate //might// result in an error being generated. The **catch** clause contains the code you want to be executed if an error occurs whilst executing the **try** clause. Whether or not an error occurs, execution continues normally after the **catch** clause.\\ \\ BBC BASIC does not natively provide such a facility, but in //BBC BASIC for Windows// you can emulate the behaviour quite easily as follows:\\ \\ | + | </ |
+ | The **try** clause contains the code you want to execute, which may consist of any number of statements and may optionally call subroutines. This is the code that you anticipate //might// result in an error being generated. The **catch** clause contains the code you want to be executed if an error occurs whilst executing the **try** clause. Whether or not an error occurs, execution continues normally after the **catch** clause.\\ \\ BBC BASIC does not natively provide such a facility, but in //BBC BASIC for Windows// and //BBC BASIC for SDL 2.0// you can emulate the behaviour quite easily as follows:\\ \\ | ||
+ | <code bb4w> | ||
ON ERROR LOCAL IF FALSE THEN | ON ERROR LOCAL IF FALSE THEN | ||
REM. ' | REM. ' | ||
Line 18: | Line 21: | ||
REM. Code to execute if an error occurred in the ' | REM. Code to execute if an error occurred in the ' | ||
ENDIF : RESTORE ERROR | ENDIF : RESTORE ERROR | ||
+ | </ | ||
Note particularly the unusual compound statement **"ON ERROR LOCAL IF FALSE THEN" | Note particularly the unusual compound statement **"ON ERROR LOCAL IF FALSE THEN" | ||
+ | <code bb4w> | ||
ON ERROR LOCAL IF FALSE THEN | ON ERROR LOCAL IF FALSE THEN | ||
REM. ' | REM. ' | ||
Line 31: | Line 36: | ||
ENDCASE | ENDCASE | ||
ENDIF : RESTORE ERROR | ENDIF : RESTORE ERROR | ||
+ | </ | ||
In this example the user can enter an expression, which is evaluated by BASIC and the result printed. If an expected error occurs during the evaluation of the expression (for example if the user entered **1/0**) then a message appropriate to the error is printed. If an unexpected error occurs it is ' | In this example the user can enter an expression, which is evaluated by BASIC and the result printed. If an expected error occurs during the evaluation of the expression (for example if the user entered **1/0**) then a message appropriate to the error is printed. If an unexpected error occurs it is ' | ||
+ | <code java> | ||
{ | { | ||
- | throw new StrangeException ("Oh dear" | + | throw new StrangeException(" |
} | } | ||
+ | </ | ||
In BBC BASIC you can of course emulate this directly:\\ \\ | In BBC BASIC you can of course emulate this directly:\\ \\ | ||
+ | <code bb4w> | ||
ON ERROR LOCAL IF FALSE THEN | ON ERROR LOCAL IF FALSE THEN | ||
REM. ' | REM. ' | ||
Line 50: | Line 59: | ||
ENDCASE | ENDCASE | ||
ENDIF : RESTORE ERROR | ENDIF : RESTORE ERROR | ||
+ | </ | ||
Here an error is generated if the user enters nothing, and this is handled by the **catch** clause.\\ \\ If the code contained in the **try** clause includes a function or procedure call, and if an error occurs in that function or procedure (or any that it calls), then any **formal parameters** and **LOCAL** variables will //not be automatically restored// to the values they had before the call. If this is a problem **RESTORE LOCAL** can be used within the function or procedure (see [[/ | Here an error is generated if the user enters nothing, and this is handled by the **catch** clause.\\ \\ If the code contained in the **try** clause includes a function or procedure call, and if an error occurs in that function or procedure (or any that it calls), then any **formal parameters** and **LOCAL** variables will //not be automatically restored// to the values they had before the call. If this is a problem **RESTORE LOCAL** can be used within the function or procedure (see [[/ | ||
+ | <code bb4w> | ||
DEF PROCmightfail(parameters) | DEF PROCmightfail(parameters) | ||
LOCAL list_of_locals | LOCAL list_of_locals | ||
Line 57: | Line 68: | ||
REM. Code which might result in an error | REM. Code which might result in an error | ||
ENDPROC | ENDPROC | ||
+ | </ |
structured_20exception_20handling.1522502385.txt.gz · Last modified: 2024/01/05 00:16 (external edit)