| 1 | # Exception Handling Changes
|
|---|
| 2 |
|
|---|
| 3 | This proposal changes the C∀ exception handling mechanism from matching an object type to a function type.
|
|---|
| 4 | Using functions fits with function overloading in the C∀ type system.
|
|---|
| 5 |
|
|---|
| 6 | The following shows a resumption example.
|
|---|
| 7 |
|
|---|
| 8 | ```
|
|---|
| 9 | exception int resumpt( int, double ); // return result, no stack unwinding
|
|---|
| 10 | try {
|
|---|
| 11 | i = resumpt( 3, 3.5 ); // exception raise
|
|---|
| 12 | } catch( int resumpt( int i, double d ) { // i = 3, d = 3.5
|
|---|
| 13 | ... return 42; // return value to raise point
|
|---|
| 14 | }
|
|---|
| 15 | ```
|
|---|
| 16 |
|
|---|
| 17 | The following shows a termination example.
|
|---|
| 18 |
|
|---|
| 19 | ```
|
|---|
| 20 | exception void termin( double ); // void return, stack unwinding
|
|---|
| 21 | try {
|
|---|
| 22 | termin( 3.5 ); // exception raise
|
|---|
| 23 | } catch( void termin( int i, double d ) { // i = 3, d = 3.5
|
|---|
| 24 | ... // fall through to lexical block
|
|---|
| 25 | }
|
|---|
| 26 | ```
|
|---|
| 27 |
|
|---|
| 28 | There is no explicit *raise* statement.
|
|---|
| 29 | (See Mesa Language Manual page 135, footnote)
|
|---|
| 30 | The compiler knows a function is an exception, so it does a search call (propagation) instead of a branch call.
|
|---|
| 31 |
|
|---|
| 32 | An exception function is denoted by the `exception` keyword to differentiate it from a regular function.
|
|---|
| 33 | An exception function is defined in a `catch` clause, which is a local, inline implementation of the exception function.
|
|---|
| 34 | The catch clause must specify the exception-function name and type for matching, and arbitrary parameter names to access the raise arguments.
|
|---|
| 35 | Data is carried from the raise call to the `catch` body via the argument/parameter mechanism, and data can be returned to the raise call via function return.
|
|---|
| 36 | An exception function cannot be passed as a function pointer nor can a raise use a pointer to an exception function.
|
|---|
| 37 |
|
|---|
| 38 | Overloading of the exception raise occurs among existing exception functions.
|
|---|
| 39 |
|
|---|
| 40 | ```
|
|---|
| 41 | exception int fix( int, double );
|
|---|
| 42 | exception double fix( int, double );
|
|---|
| 43 | exception double fix( double );
|
|---|
| 44 | try {
|
|---|
| 45 | int i = fix ( 3, 3.5 ); // choose best overloading
|
|---|
| 46 | } catch( int fix( int i, double d ) ) { // match with exact catch body
|
|---|
| 47 | } catch( double fix( int i, double d ) ) {
|
|---|
| 48 | } catch( double fix( double d ) ) {
|
|---|
| 49 | }
|
|---|
| 50 | ```
|
|---|
| 51 |
|
|---|
| 52 | Normal overload resolution occur at the raise point, where the best fit *exception* function is chosen based on arguments and left-hand side.
|
|---|
| 53 | Matching between raise and catch is exact, as for function pointers, with the selected overload chosen at the raise point (compare mangled names dynamically)
|
|---|
| 54 |
|
|---|
| 55 | An exception function has a *kind*: return, noreturn, or dual, denoting resumption, termination, or both.
|
|---|
| 56 | An exception function cannot be overloaded on the exception kind.
|
|---|
| 57 | A return exception function must return a type, a noreturn exception function must return `void`, a dual exception function must return a type.
|
|---|
| 58 | A dual exception function is specified using the quasi-keyword `dual`.
|
|---|
| 59 |
|
|---|
| 60 | ```
|
|---|
| 61 | exception int dual( int, double ) dual; // Dual, return result or exit handler block, no initial stack unwinding
|
|---|
| 62 | ```
|
|---|
| 63 |
|
|---|
| 64 | Within the handler for kind return (stack not unwound), the handler *must* return a value as for any value returning function;
|
|---|
| 65 | any other control-flow statement or fallthrough is an error, like `break` or fallthrough on a value returning function.
|
|---|
| 66 |
|
|---|
| 67 | ```
|
|---|
| 68 | } catch( int fix( int i, float j ) ) { // stack not unwound, i and j accessible, must return
|
|---|
| 69 | return 42; // implies return to fix call, not return of nested function
|
|---|
| 70 | break; // syntax error => must return
|
|---|
| 71 | // fallthrough => syntax error
|
|---|
| 72 | }
|
|---|
| 73 | ```
|
|---|
| 74 |
|
|---|
| 75 | Within the handler for kind noreturn (stack unwound), `break` or fallthrough ⇒ exit handler, like in the `case` clause of a switch statement;
|
|---|
| 76 | any other control-flow statement works on the lexical context outside the handler.
|
|---|
| 77 |
|
|---|
| 78 | ```
|
|---|
| 79 | } catch( void recover( int i ) ) { // stack unwound, cannot return
|
|---|
| 80 | break; // implies exit catch routine
|
|---|
| 81 | return; // implies return from lexical function
|
|---|
| 82 | // fallthrough => implies break
|
|---|
| 83 | }
|
|---|
| 84 | ```
|
|---|
| 85 |
|
|---|
| 86 | Within the handler for kind dual (stack not unwound), `break` or fallthrough ⇒ exit handler and unwind stack, or return a value to the raise at top of the stack.
|
|---|
| 87 |
|
|---|
| 88 | ```
|
|---|
| 89 | } catch( int dual( int i, float j ) ) { // stack not unwound, can return
|
|---|
| 90 | return 42; // implies return to fix call, not return of nested function
|
|---|
| 91 | break; // implies exit catch body
|
|---|
| 92 | // fallthrough => implies break
|
|---|
| 93 | }
|
|---|
| 94 | ```
|
|---|
| 95 |
|
|---|
| 96 | In all cases, a handler can raise another exception.
|
|---|
| 97 |
|
|---|
| 98 | An exception function can have a static body, which define the action if the exception is not caught.
|
|---|
| 99 | If no body is specified, there are default actions added.
|
|---|
| 100 | This matches with `defaultResume` and `defaultTerminate` in current C∀/μC++.
|
|---|
| 101 | Here a parameter name is necessary to access the raise argument(s).
|
|---|
| 102 |
|
|---|
| 103 | Resumption default:
|
|---|
| 104 |
|
|---|
| 105 | ```
|
|---|
| 106 | exception int resumpt( int i ) { // called if no handler found
|
|---|
| 107 | // return default correction or abort or raise another exception
|
|---|
| 108 | // default if not specified is to call UnhandledException at resumer or joiner.
|
|---|
| 109 | }
|
|---|
| 110 | ```
|
|---|
| 111 |
|
|---|
| 112 | Termination default:
|
|---|
| 113 |
|
|---|
| 114 | ```
|
|---|
| 115 | exception void termin( double d ) { // called if no handler found
|
|---|
| 116 | // abort or raise another exception
|
|---|
| 117 | // default if not specified is to call UnhandledException at resumer or joiner.
|
|---|
| 118 | }
|
|---|
| 119 | ```
|
|---|
| 120 |
|
|---|
| 121 | Dual default is the same as termination default, as having a default correction action is unlikely.
|
|---|
| 122 |
|
|---|
| 123 | A `throws` clause can be added to a regular function to indicate alternate outcomes.
|
|---|
| 124 |
|
|---|
| 125 | ```
|
|---|
| 126 | int foo(...) throws( int ex( int ), float ex( int ), char ex( double, double ) );
|
|---|
| 127 | ```
|
|---|
| 128 |
|
|---|
| 129 | The `throws` clause is *not* part of the function type, and hence, is not used for overloading.
|
|---|
| 130 | Functions with a `throws` clause cause are handled by a separate type-checking pass, which examines the statically call structure to determine if calls to `foo` are nested directly or indirectly within guarded blocks with matching catch clauses, e.g.:
|
|---|
| 131 |
|
|---|
| 132 | ```
|
|---|
| 133 | int bar( int i ) throws( int fixup( int ) );
|
|---|
| 134 |
|
|---|
| 135 | void foo(...) {
|
|---|
| 136 | ... i = bar( 3 ); ... // call statically nested within handler for fixup
|
|---|
| 137 | }
|
|---|
| 138 | void baz() {
|
|---|
| 139 | try {
|
|---|
| 140 | foo(...);
|
|---|
| 141 | } catch( int fixup( int ) ) {
|
|---|
| 142 | ... return 42;
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|
| 145 | ```
|
|---|
| 146 |
|
|---|
| 147 | If this type check fails, a warning is given, and a dynamic check is wrapped around the call to verify only the specified exception functions are raised.
|
|---|
| 148 | This dual approach allows all forms of reuse to exist, and is similar to checked/unchecked exceptions in Java.
|
|---|
| 149 |
|
|---|
| 150 | Resumption example:
|
|---|
| 151 |
|
|---|
| 152 | ```
|
|---|
| 153 | exception int fix( int i, float f );
|
|---|
| 154 |
|
|---|
| 155 | void foo() {
|
|---|
| 156 | try {
|
|---|
| 157 | for () {
|
|---|
| 158 | if ( ... ) x = fix( 3, 5.4 );
|
|---|
| 159 | }
|
|---|
| 160 | } catch( int fix( int i, float j ) ) {
|
|---|
| 161 | ... return 42; // fix up problem and return to raise
|
|---|
| 162 | }
|
|---|
| 163 | }
|
|---|
| 164 | ```
|
|---|
| 165 |
|
|---|
| 166 | Termination example:
|
|---|
| 167 |
|
|---|
| 168 | ```
|
|---|
| 169 | exception void end_of_file( ifstream is );
|
|---|
| 170 |
|
|---|
| 171 | void foo() {
|
|---|
| 172 | int i;
|
|---|
| 173 | try {
|
|---|
| 174 | for () {
|
|---|
| 175 | sin | i; // internally, does a call end_of_file( is ), which implicitly throws exception
|
|---|
| 176 | sout | i;
|
|---|
| 177 | }
|
|---|
| 178 | } catch( end_of_file( ifstream is ) && is == sin ) { // "is" used in predicate selection
|
|---|
| 179 | // close file
|
|---|
| 180 | }
|
|---|
| 181 | }
|
|---|
| 182 | ```
|
|---|
| 183 |
|
|---|
| 184 | Dual example:
|
|---|
| 185 |
|
|---|
| 186 | ```
|
|---|
| 187 | exception int dual( int i, float f ) dual;
|
|---|
| 188 |
|
|---|
| 189 | void baz() {
|
|---|
| 190 | try {
|
|---|
| 191 | for () {
|
|---|
| 192 | if ( ... ) x = dual( 3, 5.4 );
|
|---|
| 193 | }
|
|---|
| 194 | } catch( int dual( int i, float j ) ) {
|
|---|
| 195 | if ( ... ) return 42; // return to raise calls
|
|---|
| 196 | if ( ... ) break; // exit catch body
|
|---|
| 197 | // fallthrough // implies break
|
|---|
| 198 | }
|
|---|
| 199 | }
|
|---|
| 200 | ```
|
|---|
| 201 |
|
|---|
| 202 | I'm not sure this polymorphism is doing anything, except suggesting a common pattern.
|
|---|
| 203 | A catch clause cannot be polymorphic because there is no RTTI matching.
|
|---|
| 204 |
|
|---|
| 205 | ```
|
|---|
| 206 | forall( T ) exception void arithmetic( T op1, T op2, int retcode ) noreturn;
|
|---|
| 207 |
|
|---|
| 208 | enum FloatExceptions ! { Invalid, ZeroDiv, Overflow, Underflow, Inexact };
|
|---|
| 209 | exception double arithmetic( double op1, double op2, int retcode ) {
|
|---|
| 210 | // default abort
|
|---|
| 211 | }
|
|---|
| 212 | enum IntExceptions ! { ZeroDiv, Overflow, Underflow };
|
|---|
| 213 | exception double arithmetic( int op1, int op2, int retcode ) {
|
|---|
| 214 | // default abort
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | void xxx() {
|
|---|
| 218 | double x = 3.5;
|
|---|
| 219 | try {
|
|---|
| 220 | x = x / 0.0;
|
|---|
| 221 | } catch( arithmetic( double op1, double op2, int retcode ) ) {
|
|---|
| 222 | if ( retcode == FloatExceptions.ZeroDiv ) ...
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | int i = 3;
|
|---|
| 226 | try {
|
|---|
| 227 | i = i / 0;
|
|---|
| 228 | } catch( arithmetic( int op1, int op2, int retcode ) ) {
|
|---|
| 229 | if ( retcode == IntExceptions.ZeroDiv ) ...
|
|---|
| 230 | }
|
|---|
| 231 | }
|
|---|
| 232 | ```
|
|---|