error — Throw an error with optional identifier and formatted message values.
error constructs a diagnostic message and transfers control to the nearest compatible catch boundary. A one-input message is literal; multi-input forms decode documented escapes and apply sprintf-style formatting before throwing.
Syntax
error(msg)
error(msg, A)
error(errID, ___)
error(errorStruct)
error(correction, ___)Inputs
message- Type
StringScalar- Required
- Yes
- Description
- Error message text.
message- Type
StringScalar- Required
- Yes
- Description
- Error message template text.
A- Type
Any- Required
- Variadic
- Description
- Formatting values for the message template.
message_id- Type
StringScalar- Required
- Yes
- Description
- Message identifier.
msg_struct- Type
Any- Required
- Yes
- Description
- Struct containing identifier/message fields.
correction- Type
Any- Required
- Yes
- Description
- MATLAB correction object.
messageArguments- Type
Any- Required
- Variadic
- Description
- Identifier, message, and optional formatting values.
Errors
RunMat:error- When
- No arguments are supplied.
- Message
error: missing message argument
RunMat:error- When
- Additional arguments are supplied after an MException input.
- Message
error: additional arguments are not allowed when passing an MException
RunMat:error- When
- Additional arguments are supplied after a message-struct input.
- Message
error: additional arguments are not allowed when passing a message struct
Behavior and placement
error(msg) throws the literal text scalar without converting escape or format sequences.
error(msg,A) and error(errID,___) decode escape sequences, format replacement values, and then throw.
Documented error identifiers use colon-separated fields that each begin with a letter and otherwise contain only letters, digits, or underscores.
All eight integer classes are documented numeric formatting values. RunMat keeps typed integers authoritative through integer conversion specifiers, including uint64 values above binary64's exact range.
If every supplied input is an empty array, the call is a no-op and does not throw.
error(MException), unqualified identifier normalization, and the messageid or msg structure aliases are independent RunMat-only extensions and reject in MATLAB compatibility mode.
The documented matlab.lang.correction object families are not yet representable in RunMat and currently raise an explicit unsupported diagnostic.
An errorStruct must be scalar and contain at least one of message, identifier, or stack. RunMat accepts message and identifier independently; explicit stack reconstruction is deferred because RuntimeError does not yet accept MATLAB stack structures.
Formatting values gather to host after message and identifier validation; error is a host control-flow operation.
The legacy examples' prose outcomes describe the thrown diagnostic; error calls do not print a successful display result.
Examples
Throw a formatted error with an identifier
x = 1;
error('MyComponent:incorrectType','Input must be a char, not a %s.',class(x))Format an exact typed integer
error('Value is %u',uint64(18446744073709551615))Using error with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how error changes the result.
Run a small error example, explain the result, then change one input and compare the output.
FAQ
Does error execute on the GPU?⌄
No. MathWorks documents GPU-array inputs, but the function does not run on the GPU. RunMat gathers valid formatting values and throws from the host.
Are suggested corrections implemented?⌄
Not yet. RunMat does not currently model the three matlab.lang.correction object classes, so correction-object calls report the infrastructure gap explicitly.
Does errorStruct preserve an explicit stack?⌄
Not yet. Message and identifier fields are supported, but translating MATLAB stack structure arrays into RuntimeError call frames remains deferred.
Related Diagnostics functions
Source you can read
Every RunMat function's source is public. Read exactly how error is executed, line by line, in Rust.
- View the source for error in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.