nsresult

  • Revision slug: XPCOM_API_Reference/nsresult
  • Revision title: nsresult
  • Revision id: 492939
  • Created:
  • Creator: kscarfone
  • Is current revision? Yes
  • Comment Replaced Gecko_callout_header macro with a simple note

Revision Content

{{Xpcomapi}}

Represents a return (or error) code returned by an XPCOM function.

Description

This value is a strongly-typed enum used to represent a result code from an XPCOM function.

Note: On compilers that do not support strongly-typed enums (that is, compilers that don't support this feature of C++11), it falls back to being an unsigned 32-bit integer, as in past versions of Gecko.

Note: Prior to {{Gecko ("19.0")}}, nsresult was not strongly typed. As a result, it was possible for code to misuse it, such as returning an nsresult value from a function whose signature indicates it returns a boolean.

Because nsresult is strongly typed, code like the following will result in an error at compile time:

bool foo() {
  ...
  if (something) {
    return NS_ERROR_FAILURE;
  }
  ...
}

See also

Revision Source

<div>
 {{Xpcomapi}}</div>
<p>Represents a <a href="/en-US/docs/Table_Of_Errors" title="Table_Of_Errors">return (or error) code</a> returned by an XPCOM function.</p>
<h2 id="Description" name="Description">Description</h2>
<p>This value is a strongly-typed <code>enum</code> used to represent a result code from an XPCOM function.</p>
<div class="note">
 <p><strong>Note:</strong> On compilers that do not support strongly-typed <code>enum</code>s (that is, compilers that don't support this feature of C++11), it falls back to being an unsigned 32-bit integer, as in past versions of Gecko.</p>
</div>
<div class="note">
 <p><strong>Note:</strong> Prior to {{Gecko ("19.0")}}, <code>nsresult</code> was <strong>not</strong> strongly typed. As a result, it was possible for code to misuse it, such as returning an <code>nsresult</code> value from a function whose signature indicates it returns a <code>boolean</code>.</p>
</div>
<p>Because <code>nsresult</code> is strongly typed, code like the following will result in an error at compile time:</p>
<pre class="brush: cpp">
bool foo() {
  ...
  if (something) {
    return NS_ERROR_FAILURE;
  }
  ...
}</pre>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
 <li><a href="/en-US/docs/Table_Of_Errors" title="Table_Of_Errors">Table of errors</a></li>
</ul>
Revert to this revision