0

I'm programming up a custom class and want to throw an undefined operator error (like would occur if you executed {[]}*{[]}). I know I need to make an error structure with the message, the stack, and the identifier. It's the last that I'm having trouble with, I don't understand how MException works. I tried out the following code

myError.stack = dbstack;
myError.identifier = MException('foo:noBar','You did not foo bar.');
myError.message = ['You did not foo bar.'];
error(myError)

but get the error

Error using error
Field "identifier" of input structure must contain a valid MATLAB message identifier.

But I don't know what valid identifiers are because the help section just seems to go in circles.

Is there a list of valid identifiers that I'm missing? Or some additional resource?

asked Mar 31, 2019 at 0:53

1 Answer 1

1

The error command will create the MException object for you, and include call stack information. You should not create these yourself.

Simply call the error command with your new ID and message:

error('foo:noBar','You did not foo bar.');

If you really want to use a struct for this, put those two pieces of information into the identifier and message fields:

myError.identifier = 'foo:noBar';
myError.message = 'You did not foo bar.';
error(myError)

But this is just a more verbose version of the former. I guess this could be useful if you keep a struct array with errors, so you just need to do error(myErrors(3)), that way your messages and IDs will be consistent.

answered Mar 31, 2019 at 1:40
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.