Understanding the try-block

Hello, I'm studying C++ by 2 months and I'm having some problem with understanding the try-catch block in C++. I'm using the book : programming principles and practice using C++, here is what my book says :

"the basic idea of the exceptions is that if a function find is an error that it cannot handle, it does not return normally, instead, it throws an exception indicating what went wrong. Any direct or indirect caller can catch the exception, that is, specify what to do if the called code used throw"


What does Any direct or indirect caller can catch the exception, that is, specify what to do if the called code used throw means ?? does the author means the caller of a function or the catch function ?". I'm confused about this, Could you exaplain it to me in simple way ?

Last edited on
Suppose func1() calls func2() and func2() calls func3(). func3() throws an exception. This means that you can catch the exception in func1(), func2() or func3(). The program goes back through the set of "who called who" looking for a catch block for the exception.

Note that the "who called who" set is a runtime property. In my example, if, later in the program, func4() calls func3() then func4() could catch an exception that's thrown in func3().

It's easy to misuse any programming construct, but with exceptions, it's REALLY easy. The danger is that something important won't get done to clean up the program when the exception is thrown. Another danger is that some function will throw an exception, but the fact is poorly documented so the callers aren't expecting it. Exceptions can be wonderfully useful but, as with everything, it's important to understand the tradeoffs.
Topic archived. No new replies allowed.