Why doesn't C++ catch all exceptions in all functions?
This article mainly introduces "why C++ does not catch all exceptions in all functions". In daily operation, I believe many people have doubts about why C++ does not catch all exceptions in all functions. Xiaobian consulted all kinds of information and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "why C++ doesn't catch all exceptions in all functions". Next, please follow the editor to study!
E.17: don't try to catch all exceptions in all functions
Reason (reason)
Catching errors in a function that does not provide meaningful recovery operations can lead to code complexity and redundancy. Let the exception propagate outward until it reaches a function that can handle it. Let RAII handle the cleanup action on the unspin path.
Example, don't (negative example)
Void f () / / bad
{
Try {
/ /...
}
Catch (...) {
/ / no action
Throw; / / propagate exception
}
} Enforcement (implementation recommendations)
Flag nested try-blocks.
Tag a nested block of try code.
Flag source code files with a too high ratio of try-blocks to functions. (? Problem: define "too high")
Identify source files where the proportion of try code blocks to functions is too high.
At this point, the study of "why C++ does not catch all exceptions in all functions" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!