MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 1 | SYNOPSIS |
Zesstra | d59c389 | 2019-11-28 20:53:39 +0100 | [diff] [blame] | 2 | mixed catch(expr, expr, ...) |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 3 | mixed catch(expr, expr, ... ; modifiers) |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 4 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 5 | DESCRIPTION |
| 6 | Evaluate the expressions. If there is no error, 0 is returned. |
| 7 | If there is an error, the evaluation of the expressions stops at |
| 8 | that point, and a string with the error message is returned. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 9 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 10 | System error messages start with a leading '*', user-defined |
| 11 | error values (other than 0) as given to throw() and raise_error() are |
| 12 | returned as they are. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 13 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 14 | If at the time the catch() is encountered less than |
| 15 | __CATCH_EVAL_COST__ eval ticks are left, a runtime error will be |
| 16 | thrown from inside the catch() (and thus caught like every other |
| 17 | error) and the expressions will not be executed. The 'reserve' |
| 18 | modifier can be used to reserve a different amount of eval ticks. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 19 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 20 | The default behaviour of catch() can be changed using modifiers: |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 21 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 22 | 'nolog': Normally, the caught error will be logged in the |
| 23 | error logs for easier debugging. With this |
| 24 | modifier, the log is suppressed. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 25 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 26 | 'publish': Normally, master::runtime_error() is not called |
| 27 | for a caught error. This modifier instructs |
| 28 | catch() to call it nevertheless. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 29 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 30 | 'reserve <expr>': The expression has to evaluate to a number |
| 31 | greater than 0 and is used to determine the amount |
| 32 | of eval ticks to reserve, instead of the default |
| 33 | of __CATCH_EVAL_COST__. The minimum required |
| 34 | are 2 * __MASTER_EVAL_COST__. |
| 35 | 'limit <expr>': The expression has to evaluate to a number |
| 36 | greater than 0 and is used to limit the eval cost |
| 37 | for the evaluation of the expression. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 38 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 39 | catch() itself is not expensive as far as execution time is |
| 40 | concerned: it is about the same as a intra-object function call. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 41 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 42 | throw() is not very expensive either, but does include the |
| 43 | internal cleanup of several structures. |
Zesstra | d59c389 | 2019-11-28 20:53:39 +0100 | [diff] [blame] | 44 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 45 | Real runtime errors on the other hand are expensive regardless |
| 46 | of whether they are caught or not, as they include the generation |
| 47 | of the stack backtrace. |
| 48 | |
| 49 | catch() is not really an efun but a compiler directive. |
| 50 | |
| 51 | EXAMPLES |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 52 | object obj; |
| 53 | string err; |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 54 | if (err = catch(obj = clone_object("/foo/bar/baz"))) |
| 55 | write("Cannot clone object, reason:"+err"+\n"); |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 56 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 57 | HISTORY |
| 58 | LDMud 3.2.9 introduced the 'nolog' catch() as experimental feature. |
| 59 | LDMud 3.2.10 implemented 'nolog' as official form and added |
| 60 | 'publish'. |
| 61 | LDMud 3.3.559 moved the check regarding __CATCH_EVAL_COST__ inside |
| 62 | the catch(). |
| 63 | LDMud 3.3.560 added the 'reserve' modifier. |
| 64 | LDMud 3.6.7 added the 'limit' modifier. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 65 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 66 | SEE ALSO |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 67 | throw(E), raise_error(E), predefined(D), runtime_error(M) |