name: duk_error proto: | duk_ret_t duk_error(duk_context *ctx, duk_errcode_t err_code, const char *fmt, ...); stack: | [ ... ] -> [ ... err! ] summary: |

Push a new Error object to the stack and throw it. This call never returns.

The message property of the error object will be set to a sprintf-formatted string using fmt and the remaining arguments. The internal prototype for the created error object is chosen based on err_code. For instance, DUK_ERR_RANGE_ERROR causes the built-in RangeError prototype to be used. The valid range for user error codes is [1,16777215].

To push an Error object to the stack without throwing it, use duk_push_error_object().

Even though the function never returns, the prototype describes a return value which allows code such as:

  if (argvalue < 0) {
      return duk_error(ctx, DUK_ERR_TYPE_ERROR, "invalid argument value: %d", (int) argvalue);
  }
  

If the return value is ignored, cast to void to avoid compilation warnings:

  if (argvalue < 0) {
      (void) duk_error(ctx, DUK_ERR_TYPE_ERROR, "invalid argument value: %d", (int) argvalue);
  }
  
example: | (void) duk_error(ctx, DUK_ERR_RANGE_ERROR, "argument out of range: %d", (int) argval); tags: - error seealso: - duk_error_va - duk_throw - duk_push_error_object - duk_generic_error - duk_eval_error - duk_range_error - duk_reference_error - duk_syntax_error - duk_type_error - duk_uri_error introduced: 1.0.0