name: duk_throw proto: | duk_ret_t duk_throw(duk_context *ctx); stack: | [ ... val! ] summary: |

Throw the value on top of the stack. This call never returns.

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

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

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

  if (argvalue < 0) {
      duk_push_error_object(ctx, DUK_ERR_TYPE_ERROR, "invalid argument: %d", (int) argvalue);
      (void) duk_throw(ctx);
  }
  
example: | /* Throw a string value; equivalent to the ECMAScript code: * * throw "this string is thrown"; */ duk_push_string(ctx, "this string is thrown"); (void) duk_throw(ctx); tags: - error seealso: - duk_error - duk_push_error_object introduced: 1.0.0