Why use 'try' ' catch' to handle exception
19 views (last 30 days)
Show older comments
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 Comments
Stephen23
on 31 Jul 2023
"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
1/'x'
If you are not even sure what can throw errors, why do you need error handling?
Answers (2)
the cyclist
on 31 Jul 2023
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.
0 Comments
Stephen23
on 1 Aug 2023
Moved: Image Analyst
on 1 Aug 2023
" 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 Comments
See Also
Categories
Find more on Startup and Shutdown 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!