From 7cbab49645541af01dbd5afac98df637b02a0c0a Mon Sep 17 00:00:00 2001 From: cooldome Date: Mon, 12 Feb 2018 20:20:49 +0000 Subject: [PATCH] nested finally bug (#7207) --- compiler/ccgstmts.nim | 2 +- tests/exception/tfinally.nim | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim index c9642b74d3..c085c82db6 100644 --- a/compiler/ccgstmts.nim +++ b/compiler/ccgstmts.nim @@ -569,7 +569,7 @@ proc genBreakStmt(p: BProc, t: PNode) = lineF(p, cpsStmts, "goto $1;$n", [label]) proc genRaiseStmt(p: BProc, t: PNode) = - if p.inExceptBlock > 0: + if p.inExceptBlock > 0 and p.inExceptBlock == p.nestedTryStmts.len: # if the current try stmt have a finally block, # we must execute it before reraising var finallyBlock = p.nestedTryStmts[p.nestedTryStmts.len - 1].lastSon diff --git a/tests/exception/tfinally.nim b/tests/exception/tfinally.nim index aa469d9c03..e5315a3182 100644 --- a/tests/exception/tfinally.nim +++ b/tests/exception/tfinally.nim @@ -1,6 +1,13 @@ discard """ file: "tfinally.nim" - output: "came\nhere\n3" + output: '''came +here +3 +msg1 +msg2 +finally2 +finally1 +''' """ # Test return in try statement: @@ -17,3 +24,19 @@ proc main: int = echo main() #OUT came here 3 +#bug 7204 +proc nested_finally = + try: + raise newException(KeyError, "msg1") + except KeyError as ex: + echo ex.msg + try: + raise newException(ValueError, "msg2") + except: + echo getCurrentExceptionMsg() + finally: + echo "finally2" + finally: + echo "finally1" + +nested_finally() \ No newline at end of file