User Tools

Site Tools


structured_20exception_20handling

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

structured_20exception_20handling [2018/03/31 13:19]
127.0.0.1 external edit
structured_20exception_20handling [2018/04/17 19:00] (current)
tbest3112 Added syntax highlighting
Line 2: Line 2:
  
 //by Richard Russell, April 2008//\\ \\  Several modern programming languages (e.g. **C++**, **Java** and **Visual Basic**) provide a [[http://​en.wikipedia.org/​wiki/​Exception_handling|Structured Exception Handling]] capability. Typically this uses a syntax similar to the following:​\\ \\  //by Richard Russell, April 2008//\\ \\  Several modern programming languages (e.g. **C++**, **Java** and **Visual Basic**) provide a [[http://​en.wikipedia.org/​wiki/​Exception_handling|Structured Exception Handling]] capability. Typically this uses a syntax similar to the following:​\\ \\ 
 +<code java>
   try   try
     {     {
Line 10: Line 11:
       // Code to execute if an error occurred in the '​try'​ clause       // Code to execute if an error occurred in the '​try'​ clause
     }     }
 +</​code>​
 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// you can emulate the behaviour quite easily as follows:\\ \\ 
 +<code bb4w>
         ON ERROR LOCAL IF FALSE THEN         ON ERROR LOCAL IF FALSE THEN
           REM. '​Try'​ clause:           REM. '​Try'​ clause:
Line 18: Line 21:
           REM. Code to execute if an error occurred in the '​try'​ clause           REM. Code to execute if an error occurred in the '​try'​ clause
         ENDIF : RESTORE ERROR         ENDIF : RESTORE ERROR
 +</​code>​
 Note particularly the unusual compound statement **"ON ERROR LOCAL IF FALSE THEN"​** and the **"​RESTORE ERROR"​** on the last line.\\ \\  If you want to deal with different errors in different ways you can easily do that as follows:\\ \\  Note particularly the unusual compound statement **"ON ERROR LOCAL IF FALSE THEN"​** and the **"​RESTORE ERROR"​** on the last line.\\ \\  If you want to deal with different errors in different ways you can easily do that as follows:\\ \\ 
 +<code bb4w>
         ON ERROR LOCAL IF FALSE THEN         ON ERROR LOCAL IF FALSE THEN
           REM. '​Try'​ clause:           REM. '​Try'​ clause:
Line 31: Line 36:
           ENDCASE           ENDCASE
         ENDIF : RESTORE ERROR         ENDIF : RESTORE ERROR
 +</​code>​
 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 '​passed down' to the exception handler at the next lower level (if any).\\ \\  Languages that provide Structured Exception Handling generally also provide a mechanism for causing a specific error to occur, typically by means of a **throw** statement:​\\ \\  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 '​passed down' to the exception handler at the next lower level (if any).\\ \\  Languages that provide Structured Exception Handling generally also provide a mechanism for causing a specific error to occur, typically by means of a **throw** statement:​\\ \\ 
 +<code java>
   {   {
-    throw new StrangeException ("Oh dear"​);​+    throw new StrangeException("​Oh dear"​);​
   }   }
 +</​code>​
 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. '​Try'​ clause:           REM. '​Try'​ clause:
Line 50: Line 59:
           ENDCASE           ENDCASE
         ENDIF : RESTORE ERROR         ENDIF : RESTORE ERROR
 +</​code>​
 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 [[/​Cascaded%20error%20handling|Cascaded error handling]]):​\\ \\  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 [[/​Cascaded%20error%20handling|Cascaded error handling]]):​\\ \\ 
 +<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
 +</​code>​
structured_20exception_20handling.txt ยท Last modified: 2018/04/17 19:00 by tbest3112