Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exceptions thrown in asynchronous javascript not caught

Basically, why isn't this exception caught?

var http = require('http'),
    options = {
      host: 'www.crash-boom-bang-please.com',
      port: 80,
      method: 'GET'
    };

try {
  var req = http.request(options, function(res) {
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
      console.log('BODY: ' + chunk);
    });
  });

  req.on('error', function(e) {
    throw new Error("Oh noes");
  });
  req.end();
} catch(_error) {
  console.log("Caught the error");
}

Some people suggest that these errors need to be handled with event emitters or callback(err) (having callbacks with err, data signature isn't something I'm used to)

What's the best way to go about it?

like image 316
changelog Avatar asked May 30 '12 19:05

changelog


People also ask

What happens if a thrown exception is not caught?

What happens if an exception is not caught? If an exception is not caught (with a catch block), the runtime system will abort the program (i.e. crash) and an exception message will print to the console.

What happens when async function throws error?

Error thrown from async function is a rejected promise (1) Use . catch() chain function to catch the error within the promise chain. (2) Change testFn to an async function and use await to wait for the resolution of the rejected promise.

What are asynchronous exceptions?

An exception is described as asynchronous if either of the following applies: — the exception is not generated as a result of direct execution or attempted execution of the instruction. stream. — the return address presented to the exception handler is not guaranteed to indicate the instruction that. caused the ...

How do I stop async await try catch?

To avoid writing multiple try catch async await in a function, a better option is to create a function to wrap each try catch. The first result of the promise returns an array where the first element is the data and the second element is an error. And if there's an error, then the data is null and the error is defined.


2 Answers

When you are throwing the error the try {} block has been long left, as the callback is invoked asynchronously outside of the try/catch. So you cannot catch it.

Do whatever you want to do in case of an error inside the error callback function.

like image 159
ThiefMaster Avatar answered Oct 22 '22 12:10

ThiefMaster


As of node version 0.8 you can constrain exceptions to domains. You can constrain exceptions to a certain domain and catch them at that scope

If you're interested, I've written a small function that catches asynchronous exceptions here: Javascript Asynchronous Exception Handling with node.js. I'd love some feedback This will let you perform the following:

var http = require('http'),
    options = {
      host: 'www.crash-boom-bang-please.com',
      port: 80,
      method: 'GET'
    };

atry(function(){
  var req = http.request(options, function(res) {
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
      console.log('BODY: ' + chunk);
    });
  });

  req.on('error', function(e) {
    throw new Error("Oh noes");
  });
  req.end();
}).catch(function(_error) {
  console.log("Caught the error");
});
like image 5
Benjamin Gruenbaum Avatar answered Oct 22 '22 12:10

Benjamin Gruenbaum