Skip to content

Commit d109676

Browse files
committed
py: Reraising exception possible only in except block.
1 parent f4417a1 commit d109676

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

py/vm.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,9 @@ mp_vm_return_kind_t mp_execute_byte_code_2(const byte *code_info, const byte **i
697697
unum = *ip++;
698698
assert(unum <= 1);
699699
if (unum == 0) {
700+
if (!currently_in_except_block) {
701+
nlr_jump(mp_obj_new_exception_msg(&mp_type_RuntimeError, "No active exception to reraise"));
702+
}
700703
// This assumes that nlr.ret_val holds last raised
701704
// exception and is not overwritten since then.
702705
obj1 = nlr.ret_val;

tests/basics/try-reraise.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Re-reraising last exception with raise w/o args
1+
# Reraising last exception with raise w/o args
22

33
def f():
44
try:
@@ -10,3 +10,10 @@ def f():
1010
f()
1111
except ValueError as e:
1212
print(repr(e))
13+
14+
15+
# Can reraise only in except block
16+
try:
17+
raise
18+
except RuntimeError:
19+
print("RuntimeError")

0 commit comments

Comments
 (0)