Nest try-catchs

I want nest some try-cacth loops...I'm not sure how It works, according to the documentatio I have read I can nest as many try-catch as I want..but I dont know if all my excepcion will be thrown if I throw all of them...According to the result of my code It seems like just one expection is thrwon...and that one is the default one...I would like to throw all of them( to know if more that one exception can be handled...thanks..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  // exceptions
#include <iostream>
using namespace std;

int main () {

	try{
		try{
			throw "a";
			try{
				throw 4;
			}
			catch(int n){
				throw;
			}
		}
		catch (char a){
		throw;
		}
	
	}
	catch (int n) {
	cout << "The int exception has occurred"<<"Number"<< n<<"\n";
	}
	
  
	catch(char a){
	cout<<" The char exception has occurred"<<a<<"n";
	}
	
	catch(...){
	cout<<"The exception default has occurred";
	}
	
  return 0;
}
You're throwing a C string (a const char*) not a char. So the first block that can catch it is the catch ellipsis. Either alter the value you throw to a char or your catch blocks to catch const char* rather than char.

Andy

PS Throwing char, int, etc when playing with code to learn about exceptions is fine, but you should define custom exceptions for proper use; built-in types hsould not be thrown. Custom exceptions are usually derived from std::runtime_error, std::logic_error or std::exception.
Last edited on
I see, that's true I was sending an string instead of a char...now It's corrected, but my main question is that I'm throwing several types and I think that nesting differents try-cacth I'll have to accumullate the differents tyoes throwns, so at the end of the programm according to what I understand, differents handles functions should be executed....I dont if you see what I mean..
Does this help?

Andy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <exception>
#include <stdexcept>
using namespace std;

class my_error : public runtime_error {
public:
    my_error() : runtime_error("my_error") {}
};

class unknown_exception {
};

void test(int i) {
    try {
        try{
            if(i == 1) {
                cout << "throw a\n";
                throw "a";
            } else if(i == 2) {
                try{
                    cout << "throw 4\n";
                    throw 4;
                }
                catch(int n){
                    cout << "Caught int exception : " << n <<"\n";
                    cout << "rethrowing!\n";
                    throw;
                }
            } else if(i == 3) {
                cout << "throw my_error()\n";
                throw my_error();
            } else if(i == 4) {
                cout << "throw bad_alloc()\n";
                throw bad_alloc(); // standard exception
            } else {
                cout << "throw unknown_exception()\n";
                throw unknown_exception();
            }
        }
        catch(const char* s){
            cout << "Caught const char* exception : " << s <<"\n";
            cout << "rethrowing!\n";
            throw;
        }
    }
    catch(int n) {
        cout << "Caught int exception : " << n << "\n";
    }
    catch(const char* s){
        cout << "Caught const char* exception : " << s <<"\n";
    }
    catch(char a){
        cout << "Caught char exception : " << a <<"\n";
    }
    catch(const exception& e){
        cout << "exception : " << e.what() << "\n";
    }    
    catch(...){
        cout << "Caught unknown exception\n";
    }
}

int main () {
    for(int i = 1; i <=5; ++i) {
        test(i);
        cout << "\n";
    }
    return 0;
}
Last edited on
It can give more ideas, but I cant see if I can accumulate types thowns and handle them in one go...If I can't I can not see what advantage we can have nesting try-catchs...
I am not sure what you mean by accumulate types.

But in my little test the catch at line 56 is catching the base class of bad::alloc and my_error so I don't need a separate handler for these two errors.

Andy
When I say accumulate types I mean that in this program..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// exceptions
#include <iostream>
using namespace std;

int main () {

	try{
		try{
			throw 3;
			try{
				throw 'e';
			}
			catch(int n){
				throw;
			}
		}
		catch (char a){
		throw;
		}
	
	}
	
	catch (int n) {
	cout << "The int exception has occurred"<<"Number"<< n<<"\n";
	}
	
  
	catch(char a){
	cout<<" The char exception has occurred "<<a<<"\n";
	}
	
	catch(...){
	cout<<"The exception default has occurred";
	}
	
  return 0;
}


before than handle any excepcion I have thrown two exceptions 3 and 'e', and I was thinking that I shoudl get two handles function giving me the information I coded in them...but apparently It's always handling the first excepcion I throw always( It doesnt matter if I put a char a int or other thing)....what I'm trying to know is what's the reason of nest try-catch block, because If I cant handle several handle function in jus one go( when I say on go I mean when the programm has gone forward than the last brace og the first try...
I know that you're trying to show my a more accurate and general way to handle exceptions but I'm trying to see why's that....
And I'm not sure If what I'm saying has logic or not...but according tot he documentation in ..http://www.cplusplus.com/doc/tutorial/exceptions/ It explains that we can use nest try-catch but actuallu It doesnt explain why...
Topic archived. No new replies allowed.