Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Components.lastResult

Components.lastResult returns the numeric nsresult code that was the result code of the last XPCOM method called via XPConnect.

Introduction

Generally, Components.lastResult is only useful for testing the result of XPCOM methods that can return interesting 'success' codes. This is because failure result codes get converted by XPConnect into exceptions that are thrown into the calling JavaScript method. Most interfaces only return one success code -- NS_OK -- so Components.lastResult is rarely necessary.

In cases where Components.lastResult is used, it is best to get it right away after the target call is made and save it in a local variable to test its value rather than doing multiple tests against Components.lastResult. This is because many 'Components' properties and methods are themselves implemented using XPConnect and subsequent calls to Components.lastResult might reflect the result of 'implicit' XPConnect calls rather than the result of the target call.

Example

In the following example, the local variable i contains the actual result returned by bar() (assuming that bar() is called via XPConnect), and Components.lastResult contains the success code returned by bar().

// Given that foo.bar is a method that might return 
// the success codes NS_OK, '5', and '6' OR some error code...
try
{
  var i = foo.bar();

  switch (Components.lastResult)
  {
    case Components.results.NS_OK:
      // NS_OK is good!
      break;
    case 5:
      // do something with 5 here
      break;
    case 6:
      // do something with 6 here
      break;
    default:
      // this was a success code we did not expect. Bad component!
      break;
  }
  // and so on....
}
catch (e)
{
  // the call threw an exception or a native component returned 
  // a failure code!
  if (e instanceof Components.interfaces.nsIXPCException)
  {
    // we might do something interesting here with the exception object
    var rv = e.result;
  }
  else
  {
    // if we don't know how to handle it then rethrow
    throw e;
  }
}

Document Tags and Contributors

 Contributors to this page: teoli, Mgjbot, Waldo, Andreas Wuest
 Last updated by: Mgjbot,