Using exceptions in node.js is pointless because of its aynchronous nature. I’ll try to show you why.

Destined to fail

Here’s simple dot server code. Everything what may brake is in try/catch clause:

dotServer = ->
    console.log "."
    setTimeout dotServer, 1000


try console.log “zero seconds” setTimeout -> console.log “four seconds” throw error , 4000 console.log “zero seconds second time” throw error console.log “never happens” catch error console.log “exception!”

console.log “out of try/catch clause”


zero seconds
zero seconds second time
out of try/catch clause
four seconds

      throw error;

As you can see once we’re out of try/catch clause, throwing exception causes process to exit. So much for our dot server.

When to use exception

Exceptions are perfectly legitimate in synchronous parts of code. For example:

    console.log "processing JSON string"
    res = JSON.parse "{wtf"
    console.log "got it!"
catch SyntaxError
    console.log "wrong syntax!"

This results in:

processing JSON string
wrong syntax!