Skip to content

Commit 3d068ab

Browse files
committed
FIX: TRY/except should store the last-error
Now one can access the last error from exception block handler: ``` >> x: 0 try/except [1 / 0][ if 'zero-divide = system/state/last-error/id [ print "OK" x: 1] ] x OK == 1 ``` Fixes: Oldes/Rebol-issues#2419
1 parent 2435e88 commit 3d068ab

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/core/n-control.c

+4
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,10 @@ enum {
837837

838838
if (Try_Block(VAL_SERIES(D_ARG(1)), VAL_INDEX(D_ARG(1)))) {
839839
if (except) {
840+
// save it for access from the block handler or explain
841+
REBVAL *val = Get_System(SYS_STATE, STATE_LAST_ERROR);
842+
*val = *DS_NEXT;
843+
840844
if (IS_BLOCK(&handler)) {
841845
DO_BLK(&handler);
842846
}

src/tests/units/evaluation-test.r3

+17
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,23 @@ Rebol [
471471
===end-group===
472472

473473

474+
===start-group=== "TRY/except"
475+
--test-- "try/except [1 / 0] block!"
476+
;@@ https://github.com/Oldes/Rebol-issues/issues/2419
477+
system/state/last-error: none
478+
try/except [1 / 0][
479+
--assert error? system/state/last-error
480+
--assert system/state/last-error/id = 'zero-divide
481+
]
482+
--test-- "try/except [1 / 0] function!"
483+
system/state/last-error: none
484+
--assert string? try/except [1 / 0] :mold
485+
--assert system/state/last-error/id = 'zero-divide
486+
487+
488+
===end-group===
489+
490+
474491
===start-group=== "delta-profile"
475492

476493
--test-- "delta-profile []"

0 commit comments

Comments
 (0)