Why didn't this 'try.. catch' work?

If at the prompt I enter anything other than a numerical value or previously defined variable, the 'try.. catch' block does not throw an exception. Why?
try
user_input=input('Please enter a NUMERICAL input:..\n');
catch ME1
ME1
end
disp('Life goes on..');
Instead it just keep prompting me to enter an input, until I enter a valid one

 採用された回答

Walter Roberson
Walter Roberson 2016 年 6 月 8 日

2 投票

Read the input as a string and validate it yourself, and then you will have control over the behavior if a non-numeric value is entered.

その他の回答 (1 件)

Jos (10584)
Jos (10584) 2016 年 6 月 8 日

1 投票

You should read the documentation of input which says:
" If the user enters an invalid expression at the prompt, then MATLAB displays the relevant error message, and then redisplays the prompt."

7 件のコメント

SP Lee
SP Lee 2016 年 6 月 8 日
Yes I read that. If I run this at the command prompt
user_input=input('Please enter a NUMERICAL input:..\n');
and enter an invalid input, it will throw me a red error, and then prompt me to enter again until the input is valid. Why isn't there any exception if it is in a script?
SP Lee
SP Lee 2016 年 6 月 8 日
Ok it will redisplay the prompt, but it the try..catch should have catch the exception before the prompt is redisplayed, right?
Guillaume
Guillaume 2016 年 6 月 8 日
"MATLAB displays the relevant error message" that's all it does, display. It does not throw an error, therefore there is no error to catch.
SP Lee
SP Lee 2016 年 6 月 8 日
I have to disagree with this:
"MATLAB displays the relevant error message" that's all it does, display. It does not throw an error, therefore there is no error to catch.
If you first enter an invalid input, e.g 'q' in the following case, an error is "displayed", and it prompts again. At this time if you CTRL-C to break the program, and then check the last exception by typing "MException.last", you will see that it actually DID throw an Exception!
>> user_input=input('Please enter a NUMERICAL input:..\n');
Please enter a NUMERICAL input:..
q
Error using input
Undefined function or variable 'q'.
Please enter a NUMERICAL input:..
>> MException.last
ans =
MException with properties:
identifier: 'MATLAB:UndefinedFunction'
message: 'Undefined function or variable 'q'.'
cause: {0x1 cell}
stack: [0x1 struct]
>>
Walter Roberson
Walter Roberson 2016 年 6 月 8 日
So it caught the error and handled it. You are not going to have any success overriding that "catch".
Guillaume
Guillaume 2016 年 6 月 8 日
@SP Lee, from the point of view of the calling function there is no exception thrown by input and thus no exception to catch. Most likely, the way input does this is that it has its onw try_catch hence why MException.last gets changed. The calling code will never see this exception.
The fact that MException.last gets changed is a leakage of the implementation details of input, not something you should rely on. Unfortunately, there's plenty of such leakages in matlab.
Do what Walter told you to do, grab the input as a string and do your own validation:
user_input=input('Please enter a NUMERICAL input:..\n', 's'); %s to get a string
try
eval(sprintf('user_input = %s', user_input));
catch ME1
ME1
end
disp('Life goes on..');
SP Lee
SP Lee 2016 年 6 月 8 日
Agree... Thanks all!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

質問済み:

2016 年 6 月 8 日

コメント済み:

2016 年 6 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by