GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Jan-2007, 06:27
mocherla14 mocherla14 is offline
New Member
 
Join Date: Jan 2007
Posts: 1
mocherla14 is on a distinguished road

unable to jump from one catch to other catch block.


Suppose we have
Code:
main() { try{ while(error) throw 1st excep } catch(1st excep) { if (condition) we have to jump to 2nd excep is it possible? i am unable to send from this catch to next catch. pls help me with codin/example..........? } catch(2nd excep) { ...... } }
Last edited by LuciWiz : 09-Jan-2007 at 08:11. Reason: Added [code] tags
  #2  
Old 09-Jan-2007, 11:03
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: unable to jump from one catch to other catch block.


OK, I've supposed it. Now what?

And please format your code so it can be followed.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #3  
Old 09-Jan-2007, 11:44
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,031
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: unable to jump from one catch to other catch block.


Quote:
Originally Posted by mocherla14
Suppose we have
Code:
main() { try{ while(error) throw 1st excep } catch(1st excep) { if (condition) we have to jump to 2nd excep is it possible? i am unable to send from this catch to next catch. pls help me with codin/example..........? } catch(2nd excep) { ...... } }

The question you need to ask yourself is whether you want to actually jump to the block of code in the second catch, or to take the action you would in the second catch as well.

If the answer is the first, I guess you can only accomplish it by using the infamous "goto" statement - though I don't know why you would want that.

If the answer is the second, then you can simply implement the behavior into a function, and call it for both the first and the second catch:

CPP / C++ / C Code:
#include <iostream>

class ex1
{};
class ex2
{};

void DoStuff1()
{
	std::cout << "First action(s) to take" << std::endl;
}

void DoStuff2()
{
	std::cout << "Second action(s) to take" << std::endl;
}

int main()
{
	try
	{
		std::cout << "In try" << std::endl;
		ex1 exception1;
		throw exception1;
	}
	catch ( const ex1 & )
	{
		std::cout << "In first catch" << std::endl;
		DoStuff1();
		DoStuff2();
	}
	catch ( const ex2 & )
	{
		std::cout << "In second catch" << std::endl;
		DoStuff2();
	}
}

Or, you could have nested try - catch statements, and throw an exception of type "ex2" thrown from the catch of the first try statement - a bit counter-intuitive, I guess.

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to catch exception in native code QED C++ Forum 1 25-Jan-2006 13:12
Strange C++ code memory leakage problem gaoanyu C++ Forum 7 04-Nov-2005 08:09
Having a problem Chuckles Computer Hardware Forum 19 13-Sep-2004 12:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:16.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.