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..
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.
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..
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...
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.
// exceptions
#include <iostream>
usingnamespace 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...
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...