Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I have such code (roughly):

C++
1. try
2. {
3. 
4. AddMenuItem("Item1", Item1Handler);
5. AddMenuItem("Item2", Item2Handler);
6. //...
7. 
8. }catch(...)
9. {
10 // Let user know about error
11. }


Now, on Item1Handler:
C++
void MyClass::Item1Handler()
{

try
{
  if(smth) throw 11;
}
catch(someExceptionClass a)
{
}


}


Item1Handler gets called when user clicks Item1 for instance.

The problem I have now is that I was hoping the throw 11; exception would be caught in the outer (first) catch, i.e., the (...) catch -- but it does not, any clues why?

Thank you.
Posted
Updated 4-Jun-13 22:49pm
v3
Comments
AlphaDeltaTheta 5-Jun-13 4:12am    
How can you throw 11? Throw in c++, i'm not clear but absolutely may not allow integers
CHill60 5-Jun-13 4:38am    
AlphaDeltaTheta 5-Jun-13 4:46am    
Oh!... in exception handling mechanism of c++, i'm out of ideas then!

At the time you are executing the AddItem instructions, your item handlers will not be called; they are just being registered as handler for the event that the user selects such item. Hence, the try / catch(...) block can only catch problems with the AddItem itself, not the handler execution.

When the user later selects a menu item and Item1Handler is being executed, you are no longer in that try / catch(...) block, but only in the try / catch (someExceptionClass a) block. Now, if you throw the object "11", i.e. an int value, that cannot be caught by the catch clause, as you are not looking for int excpetions, but for someExceptionClass objects.
 
Share this answer
 
Comments
Dan page 5-Jun-13 4:35am    
Hi nv3 thanks, I think this is the main problem: "When the user later selects a menu item and Item1Handler is being executed, you are no longer in that try / catch(...) block"
Short answer: Your handler code is not running inside your first try/catch block.

Cheers,
Peter
 
Share this answer
 
Comments
Dan page 5-Jun-13 4:11am    
Hi, Peter, can you elaborate a bit more?
To translate several of the comments / solutions above into your code ...
C#
void MyClass::Item1Handler()
{
   try
   {
     if(smth) throw 11;
   }
   catch(...)   // handle the correct error here not someExceptionClass
   {
      throw // Don't "swallow" the error here, "bubble" it up
   }
}>/pre>
 
Share this answer
 
Comments
Dan page 5-Jun-13 4:48am    
Are you sure this will let my "FIRST" catch(...) block (e.g. line 8), catch this exception? Check nv3's comment too.
CHill60 5-Jun-13 5:29am    
The exception first gets caught within Item1Handler ... your catch block there needs to rethrow the same exception. It is the rethrown exception that will be caught by your first block (line 8). nv3 was also pointing out that your original catch block in Item1Handler wasn't trying to catch the integer exception that you had thrown because you were catching a different exception class
Dan page 5-Jun-13 8:03am    
why will rethrown exception be caught by line 8 exception? as nv3 pointed out and as it seems to make sense, my first code snippet and second one are not related, e.g., they are not stacked. Due to handlers etc.
CHill60 5-Jun-13 8:29am    
You're right I'm wrong ... I thought the code missing from the first code snippet was including the actual calls to Item1Handler. VectorX obviously thought the same thing but nv3 spotted it. After you've had a chance to read this I'll delete my solution for the sake of any googlers ... and go give nv3 a +5!
Dan page 5-Jun-13 10:01am    
ok, just I didn't test any of the solutions yet (suggested ones)
Add a throw; in catch to continue bubble.
 
Share this answer
 
that is your problem. You should throw an exception not a number.

I really dont care about such "sh*t" but:

catch( int n )
{
//do thumb things
}

I recommand NOT to use this code but for learning ;-)
 
Share this answer
 
Comments
Philippe Mori 5-Jun-13 23:06pm    
You have one good point. Except for educational purpose in small program, an application should never throw an exception that does not derive from predefined exception (std::exception for plain C++).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900