Hi, I'm having trouble using custom exceptions; any clue what I'm doing wrong here:
mymodule.nim:
type myModuleException* = ref object of Exception
extraData : uint32
proc myModuleFunc*() =
var ex : myModuleException
new(ex)
ex.extraData = 12
raise ex
main.nim:
import mymodule
try:
mymoduleFunc()
except myModuleException as ex:
echo ex.extraData
When compiling (with nim 1.0.4) I get:
user@host:~/temp/nimExceptions$ nim c -r main.nim
Hint: used config file '/home/user/.choosenim/toolchains/nim-1.0.4/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: widestrs [Processing]
Hint: io [Processing]
Hint: main [Processing]
Hint: mymodule [Processing]
/home/user/temp/nimExceptions/main.nim(7, 10) Error: undeclared field: 'extraData'
Nim version:
user@host:~/temp/nimExceptions$ nim -v
Nim Compiler Version 1.0.4 [Linux: amd64]
Compiled at 2019-11-27
Copyright (c) 2006-2019 by Andreas Rumpf
git hash: c8998c498f5e2a0874846eb31309e1d1630faca6
active boot switches: -d:release
Exporting the type is not enough. You need to mark the fields you want to export with an * too. So, here, put an * after "extraData":
type myModuleException* = ref object of Exception
extraData* : uint32