mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-07 13:33:22 +00:00
* Error -> Defect for defects The distinction between Error and Defect is subjective, context-dependent and somewhat arbitrary, so when looking at an exception, it's hard to guess what it is - this happens often when looking at a `raises` list _without_ opening the corresponding definition and digging through layers of inheritance. With the help of a little consistency in naming, it's at least possible to start disentangling the two error types and the standard lib can set a good example here.
49 lines
770 B
Nim
49 lines
770 B
Nim
discard """
|
|
cmd: "nim c --gc:arc --exceptions:goto --panics:off $file"
|
|
output: '''prevented!
|
|
caught
|
|
AssertionDefect
|
|
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")
|
|
obj.kind = kindB
|
|
obj.i = 2
|
|
try:
|
|
var objA = ObjA()
|
|
bplease(ObjB(objA))
|
|
except ObjectConversionDefect:
|
|
echo "prevented!"
|
|
|
|
try:
|
|
takeRange(i)
|
|
except RangeDefect:
|
|
echo "caught"
|
|
|
|
try:
|
|
helper()
|
|
except AssertionDefect:
|
|
echo "AssertionDefect"
|
|
|
|
echo i * i
|
|
|
|
main(30)
|