catchable defects (#13626)
* allow defects to be caught even for --exceptions:goto (WIP) * implemented the new --panics:on|off switch; refs https://github.com/nim-lang/RFCs/issues/180 * new implementation for integer overflow checking * produce a warning if a user-defined exception type inherits from Exception directly * applied Timothee's suggestions; improved the documentation and replace the term 'checked runtime check' by 'panic' * fixes #13627 * don't inherit from Exception directly
This commit is contained in:
parent
14b2354b7d
commit
a6682de004
35 changed files with 441 additions and 142 deletions
52
tests/destructor/tgotoexceptions7.nim
Normal file
52
tests/destructor/tgotoexceptions7.nim
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
discard """
|
||||
cmd: "nim c --gc:arc --exceptions:goto --panics:off $file"
|
||||
output: '''field error prevented
|
||||
prevented!
|
||||
caught
|
||||
AssertionError
|
||||
900'''
|
||||
"""
|
||||
|
||||
type
|
||||
E = enum
|
||||
kindA, kindB
|
||||
Obj = object
|
||||
case kind: E
|
||||
of kindA: s: string
|
||||
of kindB: i: int
|
||||
|
||||
ObjA = ref object of RootObj
|
||||
ObjB = ref object of ObjA
|
||||
|
||||
proc takeRange(x: range[0..4]) = discard
|
||||
|
||||
proc bplease(x: ObjB) = discard
|
||||
|
||||
proc helper = doAssert(false)
|
||||
|
||||
proc main(i: int) =
|
||||
var obj = Obj(kind: kindA, s: "abc")
|
||||
try:
|
||||
obj.kind = kindB
|
||||
except FieldError:
|
||||
echo "field error prevented"
|
||||
|
||||
try:
|
||||
var objA = ObjA()
|
||||
bplease(ObjB(objA))
|
||||
except ObjectConversionError:
|
||||
echo "prevented!"
|
||||
|
||||
try:
|
||||
takeRange(i)
|
||||
except RangeError:
|
||||
echo "caught"
|
||||
|
||||
try:
|
||||
helper()
|
||||
except AssertionError:
|
||||
echo "AssertionError"
|
||||
|
||||
echo i * i
|
||||
|
||||
main(30)
|
||||
Loading…
Add table
Add a link
Reference in a new issue