-2

I am trying to get on the screen: 1,2,3,4,5,6,7, but I don't know how to order the catch(s) in my program. Is it possible to get this output by ordering the catches? Can you please explain how it works? I searched in many forums, but I have found nothing about that. Thank you so much.

#include <exception>
#include <string>
#include <iostream>
using namespace std;
class My_Exception : public exception {};
class something {};
void func1() { cout << "1" << endl; throw exception(" no t h ing "); }
void func2() { cout << "2" << endl; throw string(" 12345 "); }
void func3() { cout << "3" << endl; throw out_of_range(" abcde "); }
void func4() { cout << "4" << endl; throw ios::failure(" i o s "); }
void func5() { cout << "5" << endl; throw My_Exception(); }
void func6() { cout << "6" << endl; throw something(); }
void func7() { cout << "7" << endl; throw logic_error(" t r u e "); }
int main() {
try {
func1(); func2(); func3();
func4(); func5(); func6();
func7();
}
catch (My_Exception) {/*1*/ }
catch (exception) {/*2*/ }
catch (logic_error) {/*3*/ }
catch (string) {/*4*/ }
catch (...) {/*5*/ }
catch (out_of_range) {/*6*/ }
catch (ios::failure) {/*7*/ }
catch (something) {/*8*/ }
return 0;
}
VLL
  • 9,634
  • 1
  • 29
  • 54
Joe
  • 31
  • 9
  • What's your question? – user253751 Dec 10 '15 at 08:24
  • the order of catch instructions in main function, in order to print : 1 2 3 4 5 6 7 if you run this code, you will see just the number "1" – Joe Dec 10 '15 at 08:26
  • 1
    @immibis Probably those sentences that end with a question mark... – skyking Dec 10 '15 at 08:28
  • Catching a C++ exception doesn't make the execution resume at the throwing point. As I recall Microsoft argued for resumable exceptions during the standardization process, but nobody else wanted that. The low level Windows SEH exceptions are resumable. – Cheers and hth. - Alf Dec 10 '15 at 08:31
  • 1
    Welcome to stackoverflow! You need to read more on exceptions. After the call to `func1()` an exception is thrown, so the control is transferred to the first `catch` block that matches the type of the thrown exception. The rest of the `catch` block **is skipped**. – galdin Dec 10 '15 at 08:31
  • @ gldraphael : so is it impossible to do it in this case? – Joe Dec 10 '15 at 08:49

2 Answers2

5

Read more about C++11. Spend a few days reading a good book on Programming in C++

This try...catch block explanation tells that

When an exception of type E is thrown by any statement in compound-statement, it is matched against the types of the formal parameters T of each catch-clause in handler-seq, in the order in which the catch clauses are listed.

hence you might want to order your catch block by putting the most frequent exceptions first (but in practice, the order does not matter much, as long as there is no overlap between catch....; if there is an overlap then put the most precise catch clause first).

Since in <stdexcept> the std::logic_error is a subclass of std::exception you should put catch(logic_error) (the more precise) before catch(exception) (the less precise but overlapping catch)

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
3

When you throw an exception, the code in try block ends its execution. The program executes corresponding catch block, and then continues to execute lines that come after that. A catch block is only executed after you throw an exception.

If you want to continue execution after catching an exception, you should use multiple try blocks. The following code prints 1 and 2.

void func1() { cout << "1" << endl; throw exception(" no t h ing "); }
void func2() { cout << "2" << endl; throw string(" 12345 "); }
try{
    func1();
    cout << "this is not printed" << endl;
}
catch(exception){
    // This is executed after throw
}
catch(string){
    // This is not executed
}

try{
    func2();
}
catch(exception){
    // This is not executed
}
catch(string){
    // This is executed after throw
}
VLL
  • 9,634
  • 1
  • 29
  • 54
  • That was so helpfull... Thank you so much... So there is no way to do that with one try block? right? – Joe Dec 10 '15 at 09:55
  • @Joe Yes, you need multiple try blocks. – VLL Dec 10 '15 at 09:58
  • @Joe Order of catch blocks does not matter, if your exception classes are not related to each other. However, in your code you are catching `exception`, and also some classes that are inherited from `exception` (see http://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm ). This means if your `catch(exception)` stays in its current location, it will be reached before `catch(logic_error)` or `catch(out_of_range)`, and code in those blocks is never executed; they execute code in the `catch(exception)` block instead. So you should move that block more down. – VLL Dec 10 '15 at 10:39
  • You also have `catch(...)`, which catches everything. Any catch block that comes after it will be never executed. So `catch(...)` should always be the last. Only one catch block is executed for each `throw`. – VLL Dec 10 '15 at 10:40
  • @Joe You should also move `catch (logic_error)` before `catch(exception)`. After that it should work. Here you can find list of standard exceptions (which have been inherited from `exception` class): http://www.cplusplus.com/reference/stdexcept/ – VLL Dec 10 '15 at 12:43