Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
doc: advise *strongly* against uncaughtException
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis committed Jul 18, 2012
1 parent e1fb7b7 commit e8af340
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions doc/api/process.markdown
Expand Up @@ -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

Expand Down

7 comments on commit e8af340

@mscdex
Copy link

@mscdex mscdex commented on e8af340 Jul 18, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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
Copy link

@isaacs isaacs commented on e8af340 Jul 19, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shigeki Looks like a reasonable approach to me.

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

@shigeki
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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
Copy link

@weepy weepy commented on e8af340 Jul 26, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.