Try - Catch Exception handling routine
Show older comments
I am trying to handle exceptions gracefully such that when a user enters a character, when an number is expected, he is notified with a custom warning/message.
I know that the try, catch has the following syntax:
try
statements
catch exception
statements
end
I have been trying something like this, to no avail:
try
number = input('Enter number: ');
catch ME
warning('Can not use characters, please enter a number instead');
end
I get the expected error message if I input something like: '-', but not any other letter.
What I suspect that is happening is this: The try block does not generate an error when a character is entered and thus no error is generated. I therefore need to generate the error in the 'try' block but do not seem to manage this part. I have tried testing with isnumeric and isnan, but my implementation is flawed.
Any pointers on how I can improve my code would be greatly appreciated.
Accepted Answer
More Answers (0)
Categories
Find more on Construct and Work with Object Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!