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;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:46 次

字数:2560

最后编辑:6年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文