Effacer les filtres
Effacer les filtres

Try - Catch Exception handling routine

4 vues (au cours des 30 derniers jours)
Ryan Rizzo
Ryan Rizzo le 16 Avr 2018
Commenté : Ryan Rizzo le 16 Avr 2018
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.

Réponse acceptée

Stephen23
Stephen23 le 16 Avr 2018
Modifié(e) : Stephen23 le 16 Avr 2018

Try this:

num = str2double(input('Enter number: ','s'));
if isnan(num)
    warning(...)
end

Using try and catch is a bit of a sledgehammer approach.

  3 commentaires
Stephen23
Stephen23 le 16 Avr 2018
Modifié(e) : Stephen23 le 16 Avr 2018
@Ryan Rizzo: Your concept is not as simple as you imagine, because you want that any non-numeric input entered by the user will be an error, but this is simply not true.
If you want to force an error for any non-numeric input then you need an error to occur only for non-numeric values. However input will run quite a lot of MATLAB code quite happily, which means that it will not trigger an error for many kinds of non-numeric inputs. Creating an exhaustive list of all possible non-numeric inputs that will work without error is not feasible: it is simpler to detect which ones are numeric. So if you really really wanted to use try / catch then you would need to get the input values, check if they are numeric, and then if not force an error (otherwise an error is not guaranteed, remember!). So you would end up with quite indirect code, something like this:
num = input(...)
try
assert(isnumeric(num),...)
catch
...
end
But this is really very indirect, a bit Heath-Robinson, and it would be simpler to use code like in my answer.
This is also the reason why you should avoid calling input without the 's' option: note that input will evaluate any valid MATLAB code, and so there is not even a guarantee that the user has provided any input value at all (they might just format your hardrive though, or delete your works files, or quit MATLAB, or ...).
My answer avoids all of these issues for a simpler alternative.
Ryan Rizzo
Ryan Rizzo le 16 Avr 2018
@Stephen Cobeldick, many thanks for your in depth comment. You have made this much clearer. Thanks for the valuable information.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Debugging and Analysis dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by