macro.catchAssert: Difference between revisions
No edit summary |
|||
(4 intermediate revisions by 2 users not shown) | |||
Line 9: | Line 9: | ||
Set this variable to {{code|0}} (default assert behavior) or {{code|1}} (catch asserts). | Set this variable to {{code|0}} (default assert behavior) or {{code|1}} (catch asserts). | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[h: macro.catchAssert = 0] | [h: macro.catchAssert = 0] | ||
[h: macro.catchAssert = 1] | [h: macro.catchAssert = 1] | ||
</ | </syntaxhighlight> | ||
==Examples== | ==Examples== | ||
Line 26: | Line 26: | ||
!Default assert Macro | !Default assert Macro | ||
|- | |- | ||
|valign="top"|< | |valign="top"|<syntaxhighlight lang="mtmacro" line> | ||
[h: resultText = "defaultValue"] | [h: resultText = "defaultValue"] | ||
[h: resultText = assert(0, "error occured")] | [h: resultText = assert(0, "error occured")] | ||
[r: resultText] | [r: resultText] | ||
</ | </syntaxhighlight> | ||
|} | |} | ||
Line 41: | Line 41: | ||
!Catching assert | !Catching assert | ||
|- | |- | ||
|valign="top"|< | |valign="top"|<syntaxhighlight lang="mtmacro" line> | ||
[h: macro.catchAssert = 1] | [h: macro.catchAssert = 1] | ||
[h: resultText = "defaultValue"] | [h: resultText = "defaultValue"] | ||
Line 47: | Line 47: | ||
[r: resultText] | [r: resultText] | ||
[h: macro.catchAssert = 0] | [h: macro.catchAssert = 0] | ||
</ | </syntaxhighlight> | ||
|} | |} | ||
Line 59: | Line 59: | ||
!Calling macro catching an assert | !Calling macro catching an assert | ||
|- | |- | ||
|valign="top"|< | |valign="top"|<syntaxhighlight lang="mtmacro" line> | ||
[h: "this macro will do something and then assert"] | [h: "this macro will do something and then assert"] | ||
[h: "... doing something"] | [h: "... doing something"] | ||
[h: assert(0, "error occured")] | [h: assert(0, "error occured")] | ||
</ | </syntaxhighlight> | ||
|< | |<syntaxhighlight lang="mtmacro" line> | ||
[h: macro.catchAssert= 1] | [h: macro.catchAssert= 1] | ||
[h: resultText = "defaultValue"] | [h: resultText = "defaultValue"] | ||
Line 70: | Line 70: | ||
[r: resultText] | [r: resultText] | ||
[h: macro.catchAssert= 0] | [h: macro.catchAssert= 0] | ||
</ | </syntaxhighlight> | ||
|} | |} | ||
Line 80: | Line 80: | ||
[[Category:Special Variable]] | [[Category:Special Variable]] | ||
[[Category:Macro Function]] |
Latest revision as of 23:59, 4 July 2023
Introduced in version 1.5.0. The variable macro.catchAssert
can be used to override the behavior of the default assert() function to continue execution instead of halting.
Whenever you set macro.catchAssert
to 1
, any use of assert() in a subsequently called macro will not assert, but will trigger a return from the macro instead.
The variable macro.catchAssert
must be in the variable scope where the assert should be caught. It is not a general flag to turn off assert behavior, just temporarily in the macro or variable scope where it's set.
Usage
Set this variable to 0
(default assert behavior) or 1
(catch asserts).
[h: macro.catchAssert = 0]
[h: macro.catchAssert = 1]
Examples
When a macro is called by another macro, the called macro may use assert() to cancel execution and show an error message. Usually all macro execution would stop, so the calling macro would not continue. We can now override that default behavior by catching the assert (it will still show the error message).
Default behaviour
The macro below will only have "error occured" as output because the default behavior is that any call to assert() will stop the overall macro execution. Whatever code is defined after the call to assert() is not executed.
Default assert Macro |
---|
[h: resultText = "defaultValue"]
[h: resultText = assert(0, "error occured")]
[r: resultText] |
Catching the assert
The macro below will output "error occured" and defaultValue
as the macro.catchAssert
set to 1
prevents the call to assert() from actually terminating execution. The example shows how to set a default value for any return value of a function/macro that has an assert() call in it. The macro then re-enables the normal function of assert() again by setting macro.catchAssert
to 0
. As each macro has its own variable scope, this only affects calls to assert() within the current macro or calls to other macros from the current macro. The exception is if the called macro shares the scope with the calling macro, in which case the assert() is ignored.
Catching assert |
---|
[h: macro.catchAssert = 1]
[h: resultText = "defaultValue"]
[h: resultText = assert(0, "error occured")]
[r: resultText]
[h: macro.catchAssert = 0] |
Catching the assert with nested macro calls
Same as in the above example, the calling macro below will output "error occured" and defaultValue
because we activate deferred asserts (in anything that this macro will call afterwards) with macro.catchAssert
set to 1
.
Called macro using an assert as function doSomething() | Calling macro catching an assert |
---|---|
[h: "this macro will do something and then assert"]
[h: "... doing something"]
[h: assert(0, "error occured")] |
[h: macro.catchAssert= 1]
[h: resultText = "defaultValue"]
[h: resultText = doSomething()]
[r: resultText]
[h: macro.catchAssert= 0] |
See also
Version changes
- 1.5.0 - introduced macro.assertCatch