Try - Catch Exception handling routine
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Construct and Work with Object Arrays en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!