Create a program that demonstrates that a function with its own try block should not catch all possible exceptions thrown in the try block. Some exceptions may be issued to external scopes and processed into them.
I understand here it is necessary that when exiting the function there was another catch to handle another situation. Tell me how to implement it. There is syntactically wrong. Swears.
void Foo(int& a) { try { if (a > 0) throw 1; else throw "Digit is negative!\n\n"; } catch (int & res) { cout << "Digit is positive!\n\n"; } } void main() { int a; cout << "Enter digit:"; cin >> a; Foo(a); catch (char * res) { cout << *res << endl; } }
throwtypes that are not intended for this (be itint,char, etc.). Use special types-heirs ofstd::exception, created specifically for this purpose, or, if necessary, create your own type, inheriting from all the samestd::exception. Of course, you can also write your own base type for exceptions, but that’s another story :) - Vlad Sivirin