Why use 'try' ' catch' to handle exception

18 次查看(过去 30 天)
fa wu
fa wu 2023-7-31
评论: fa wu 2023-8-2
The teacher said that using exception testing can improve the robustness of the program. Clearly it is true.
But there is a paradox here.
If I didn't realize that this section of the program may have exception. I wouldn't use try{} either! If I realize that this program may be has exception, why don't I fix this program to make it more robust?
For example,simple function
function c=div(a,b)
c=a/b;
end
If b=null or b=0 or b is char ,there will be an exception, why do I have to use
function c=div(a,b)
try{c=a/b;
}
catch (){…………
}
end
Why do not I use a switch case + isa(b,'') to verify that the input parameters?
If you find that the bucket is leaking
try catch helps you pour the leaked water back into the bucket
But why don't I plug the bucket hole?
If you don't notice the bucket is leaking
try catch doesn't help much either
this is my confusion
  2 个评论
Stephen23
Stephen23 2023-7-31
"If b=null or b=0 or b is char ,there will be an exception"
MATLAB does not have a NULL type.
The other two do not cause exceptions:
1/0
ans = Inf
1/'x'
ans = 0.0083
If you are not even sure what can throw errors, why do you need error handling?
fa wu
fa wu 2023-8-1
It is a java code. Just an example to illustrate my problem.

请先登录,再进行评论。

回答(2 个)

the cyclist
the cyclist 2023-7-31
I think the following phrase from the documentation for try-catch is a useful characterization: "override the default error behavior for a set of program statements".
For example, you might ask a user for input that needs to be numeric. If they do not enter a numeric, you want to override the error they would get, with your own error message.
Also, try-catch can be a safeguard against a program hard-crash against unanticipated errors. ("It is difficult to make things fool-proof, because fools are so ingenious." -- anonymous)
At some point, "plugging the hole", etc, is just semantics. In the end, one uses the programming language as one sees fit.

Stephen23
Stephen23 2023-8-1
移动:Image Analyst 2023-8-1
" why don't I fix this program to make it more robust?"
This is not always possible.
Consider code which accepts a function handle and calls it with some values (e.g. an optimization routine): without running that function it is not possible to conclusively say if it will throw an error or not. If this is part of some GUI then it may not be acceptable to simply stop responding and print lots of the error text into the command line: to provide polite feedback to the user from within the GUI thus requires catching the error and updating the GUI accordingly.
Ultimately this is also the case when dealing with file systems (e.g. writing/creating files), serial devices, and other external connections which cannot be "fixed" in the MATLAB code as you claim. Some/most of the MATLAB tools that support these connections use some form of TRY/CATCH internally rather than crashing MATLAB, the OS, or whatever: MATLAB is nice enough to give you that same option on your code, so that your application can politely react in the most appropriate way.
Even different MATLAB versions can be handled neatly in this way: the alternative of robustly determining the version is often more complex and fragile than simply trying a feature to see if it exists/runs (especially when writing code for versions that do not support VERLESSTHAN, etc).
Your examples using numeric operations are not really where TRY / CATCH is required or useful.
  3 个评论
fa wu
fa wu 2023-8-2
Thanks for your help!
"Consider code which accepts a function handle and calls it with some values (e.g. an optimization routine): without running that function it is not possible to conclusively say if it will throw an error or not. If this is part of some GUI then it may not be acceptable to simply stop responding and print lots of the error text into the command line: to provide polite feedback to the user from within the GUI thus requires catching the error and updating the GUI accordingly."----------------That make sense,How do you decide where to insert the try-catch block? Rely on experience?Does this mean that you have a feeling that something might go wrong in this section, but you don't know what kind of problem it might be? Because“idiots are so ingenius!”
fa wu
fa wu 2023-8-2
For example, a program wants to print a file. But the user may not have connected the printer, and the printer may not be powered on. Maybe the printer without paper, maybe the printer exploded. You can't predict it. You know the barrel will leak, but you can't predict where the water is leaking from.So insert a try-catch block,It's like buying an insurance.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by