Skip to content

Commit 083c0de

Browse files
committed
FEAT: implemented attempt/safer to catch also thrown exceptions
resolves: Oldes/Rebol-issues#583 resolves: Oldes/Rebol-issues#1506
1 parent 6edaaa0 commit 083c0de

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

src/boot/natives.reb

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ assert: native [
5656
attempt: native [
5757
"Tries to evaluate a block and returns result or NONE on error."
5858
block [block!]
59+
/safer "Capture all possible errors and exceptions"
5960
]
6061

6162
break: native [

src/core/n-control.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ enum {
352352
***********************************************************************/
353353
{
354354
Try_Block(VAL_SERIES(D_ARG(1)), VAL_INDEX(D_ARG(1)));
355-
if (IS_ERROR(DS_NEXT) && !IS_THROW(DS_NEXT)) return R_NONE;
355+
if (IS_ERROR(DS_NEXT) && (D_REF(2) || !IS_THROW(DS_NEXT))) return R_NONE;
356356
return R_TOS1;
357357
}
358358

src/tests/units/evaluation-test.r3

+23-6
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,6 @@ Rebol [
224224
===end-group===
225225

226226

227-
===start-group=== "attempt"
228-
--test-- "issue-41"
229-
--assert none? attempt [2 / 0] ;@@ https://github.com/Oldes/Rebol-issues/issues/41
230-
231-
===end-group===
232-
233227
===start-group=== "reduce"
234228

235229
--test-- "reduce-1"
@@ -772,6 +766,29 @@ Rebol [
772766
===end-group===
773767

774768

769+
===start-group=== "attempt"
770+
--test-- "issue-41"
771+
;@@ https://github.com/Oldes/Rebol-issues/issues/41
772+
--assert none? attempt [2 / 0]
773+
774+
--test-- "attempt/safer"
775+
;@@ https://github.com/Oldes/Rebol-issues/issues/583
776+
;@@ https://github.com/Oldes/Rebol-issues/issues/1506
777+
--assert none? attempt/safer [break 'not-reached]
778+
--assert none? attempt/safer [continue 'not-reached]
779+
--assert none? attempt/safer [exit 'not-reached]
780+
--assert none? attempt/safer [return 1 'not-reached]
781+
--assert none? attempt/safer [throw 1 'not-reached]
782+
--test-- "try/all [attempt]"
783+
--assert error? try/all [attempt [break 'not-reached]]
784+
--assert error? try/all [attempt [continue 'not-reached]]
785+
--assert error? try/all [attempt [exit 'not-reached]]
786+
--assert error? try/all [attempt [return 1 'not-reached]]
787+
--assert error? try/all [attempt [throw 1 'not-reached]]
788+
789+
===end-group===
790+
791+
775792
===start-group=== "CATCH"
776793
;@@ https://github.com/Oldes/Rebol-issues/issues/1518
777794
;@@ https://github.com/Oldes/Rebol-issues/issues/1520

0 commit comments

Comments
 (0)