Files
Nim/tests/async/tasynctry.nim
ringabout 093f5a1de5 Makes except: panics on Defect (#24821)
implements https://github.com/nim-lang/RFCs/issues/557

It inserts defect handing into a bare except branch

```nim
try:
  raiseAssert "test"
except:
  echo "nope"
```

=>

```nim
try:
  raiseAssert "test"
except:
  # New behaviov, now well-defined: **never** catches the assert, regardless of panic mode
  raiseDefect()
  echo "nope"
```

In this way, `except` still catches foreign exceptions, but panics on
`Defect`. Probably when Nim has `except {.foreign.}`, we can extend
`raiseDefect` to foreign exceptions as well. That's supposed to be a
small use case anyway.

 `--legacy:noPanicOnExcept` is provided for a transition period.

(cherry picked from commit 26b86c8f4d)
2025-04-04 10:08:49 +02:00

119 lines
2.1 KiB
Nim

discard """
output: '''
Generic except: Test
Specific except
Multiple idents in except
Multiple except branches
Multiple except branches 2
success
'''
targets: "c"
"""
import asyncdispatch, strutils
# Here we are testing the ability to catch exceptions.
proc foobar() {.async.} =
if 5 == 5:
raise newException(IndexDefect, "Test")
proc catch() {.async.} =
# TODO: Create a test for when exceptions are not caught.
try:
await foobar()
except Exception:
echo("Generic except: ", getCurrentExceptionMsg().splitLines[0])
try:
await foobar()
except IndexDefect:
echo("Specific except")
try:
await foobar()
except OSError, FieldDefect, IndexDefect:
echo("Multiple idents in except")
try:
await foobar()
except OSError, FieldDefect:
assert false
except IndexDefect:
echo("Multiple except branches")
try:
await foobar()
except IndexDefect:
echo("Multiple except branches 2")
except OSError, FieldDefect:
assert false
waitFor catch()
proc test(): Future[bool] {.async.} =
result = false
try:
raise newException(OSError, "Foobar")
except:
result = true
return
proc foo(): Future[bool] {.async.} = discard
proc test2(): Future[bool] {.async.} =
result = false
try:
discard await foo()
raise newException(OSError, "Foobar")
except:
result = true
return
proc test3(): Future[int] {.async.} =
result = 0
try:
try:
discard await foo()
raise newException(OSError, "Hello")
except:
result = 1
raise
except:
result = 2
return
proc test4(): Future[int] {.async.} =
try:
discard await foo()
raise newException(ValueError, "Test4")
except OSError:
result = 1
except:
result = 2
var x = test()
assert x.waitFor()
x = test2()
assert x.waitFor()
var y = test3()
assert y.waitFor() == 2
y = test4()
assert y.waitFor() == 2
# bug #14279
proc expandValue: Future[int] {.async.} =
return 0
proc a(b: int): Future[void] {.async.} =
return
proc b: Future[void] {.async.} =
await a(await expandValue())
echo "success"
waitFor(b())