Thanks. AFAIK this requires to write something like newMyException since I don't how to initialize the fields of MyException. Unfortunately,
newException(MyException(myfield=1))
does not work since newException expects a type argument. What is the difference of the following code snippets?
The following one is modelled after the standard newException proc
MyError= object of CatchableError
count:int
...
proc newMyError(ct:int) : owned(ref MyError ) {.noinline.} =
var e: owned(ref MyError)
new(e)
e.count= ct
return e
...
try :
raise newMyError(3)
except MyError as E :
echo "caught", E.count
Is the following alternative safe, as well?
MyError= ref object of CatchableError
count:int
...
try :
raise MyError(count:3)
except MyError as E :
echo "caught ", E.count
Usually I create an empty instance with newException, assign field values, then call raise.
I don’t understand the need for newException, to be honest. Why are exceptions special this way? I should be able to declare an exception subclass as ref, then instantiate one normally and raise it. But that produces an error.