I use try catch
block in iced coffee script
. I call not existent method fake
of not existent object a
and expect to catch error.
db = require '../../call/db.iced'
try
await db.find "79", defer c, d
a.fake()
catch error
console.log "error catched"
console.log error
But after calling function db.find
a.fake() throw error in console, but it don't use try catch
block as expected.
If I comment out string await db.find "79", defer c, d
...
db = require '../../call/db.iced'
try
# await db.find "79", defer c, d ############## commented out
a.fake()
catch error
console.log "error catched"
console.log error
... it works as expected and error is catched.
I tried to change string await db.find "79", defer c, d
by other simple async functions calles but they works fine and error was catched well.
It is interesting that function db.find
works good. When I comment out string a.fake()
...
db = require '../../call/db.iced'
try
await db.find "79", defer c, d
#a.fake() ################################ commented out
catch error
console.log "error catched"
console.log error
... this script works without any errors and so without catching errors.
Can not figure out why I can not catch error after function await db.find "79", defer c, d
.
The documentation states the following on
try catch
statements:I suspect that since
db.find
is called asynchronously, thetry
construct remains with thedb.find
thread and does not stay in the main thread. This would result in the findings you describe.One crude solution is to catch both function calls. However, I think the proper way to use
await
is with thedefer
function. Check out the documentation for an explanation.Also, you might find the following helpful:
Possible Solutions
Place a try catch around both statements.
As described in the link above, place
db.find
outside the try catch and detect it's error another way.