Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

doc: advise *strongly* against uncaughtException

  • Loading branch information...
commit e8af3405574dfee2cb8c11bf27195b774332db96 1 parent e1fb7b7
@bnoordhuis bnoordhuis authored
Showing with 12 additions and 3 deletions.
  1. +12 −3 doc/api/process.markdown
View
15 doc/api/process.markdown
@@ -43,10 +43,19 @@ Example of listening for `uncaughtException`:
console.log('This will not run.');
Note that `uncaughtException` is a very crude mechanism for exception
-handling. Using try / catch in your program will give you more control over
-your program's flow. Especially for server programs that are designed to
-stay running forever, `uncaughtException` can be a useful safety mechanism.
+handling and may be removed in the future.
+Don't use it, use [domains](domain.html) instead. If you do use it, restart
+your application after every unhandled exception!
+
+Do *not* use it as the node.js equivalent of `On Error Resume Next`. An
+unhandled exception means your application - and by extension node.js itself -
+is in an undefined state. Blindly resuming means *anything* could happen.
+
+Think of resuming as pulling the power cord when you are upgrading your system.
+Nine out of ten times nothing happens - but the 10th time, your system is bust.
+
+You have been warned.
## Signal Events

7 comments on commit e8af340

@mscdex

So if uncaughtException may be removed in the future, does that mean every node.js process will have at least 1 domain?

If so, I was under the impression domains had some kind of performance penalty? If not, it seems like dropping uncaughtException gives us no control over what happens when something blows up (by default)?

@bnoordhuis

So if uncaughtException may be removed in the future, does that mean every node.js process will have at least 1 domain?

Perhaps - details are TBD. uncaughtException is beyond redemption though, it's fundamentally broken.

If not, it seems like dropping uncaughtException gives us no control over what happens when something blows up (by default)?

You say that as if it's a bad thing. :-)

@shigeki

@bnoordhuis I think that one of alternatives is to introduce globalDomain like

var globalDomain = require('domain').globalDomain;
globalDomain.on('error', function(e) {
  console.log(e.message);
});
function f() {
  throw new Error('foo');
}
f();

This can be easily implemented as

--- a/lib/domain.js
+++ b/lib/domain.js
@@ -30,7 +30,7 @@ var endMethods = ['end', 'abort', 'destroy', 'destroySoon'];
 // communicate with events module, but don't require that
 // module to have to load this one, since this module has
 // a few side effects.
-events.usingDomains = true;
+events.usingDomains = exports.usingDomains = true;

 exports.Domain = Domain;

@@ -253,3 +253,15 @@ Domain.prototype.dispose = function() {
   // so that it can't be entered or activated.
   this._disposed = true;
 };
+
+Object.defineProperty(exports, 'globalDomain', {
+  get: function() {
+    var globalDomain = new Domain();
+    if (exports.active) {
+      stack.push(globalDomain);
+    } else {
+      globalDomain.enter();
+    }
+    return globalDomain;
+  }
+});

How do you like it?

@isaacs
Owner

Since domains is still an experimental feature, I'd prefer not to force it on people just yet. We're still working out bugs and odd edge cases, and might change its behavior significantly in 0.9, depending on what we learn from people using it over the next few months.

@bnoordhuis

@shigeki Looks like a reasonable approach to me.

[EDIT: hadn't noticed that @isaacs already commented.]

@shigeki

@isaacs The change of document in this commit indicates to use domain instead of uncaughtException event. I think we should provide alternative solutions to make the most use of domain.

@weepy

uncaughtException is useful for doing things to log exception, otherwise you don't know why something died

  process.on('uncaughtException', function(e) {
    log(e)
    process.exit()
  })
Please sign in to comment.
Something went wrong with that request. Please try again.