Problems with exceptions in C++

Started by
2 comments, last by quophyie 16 years, 1 month ago
May I have some help with this exception please. I am trying to catch an exception but everytime that I run the application I get the following error "This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information". All my other exceptions work except this one and I cannot pin point why. I thought it was to do with Microsoft and I have downloded to the hotfix for this but still getting no joy. Is there anything that I am missing. The code is as follows. Note*** Please note that DeckOfCards is a vector containing cards The code is as follows

pack::cardType Deck::getCard(){	
	resetCard(card);
 
	//deckOfCards is a vector containing  deck of cards
	// check and make sure that the deck of cards are not empty.
	if (!deckOfCards.empty()){
	if (doc_count<deckOfCards.size()){
		card = deckOfCards.at(doc_count);
		deckOfCards.erase(deckOfCards.begin(),deckOfCards.begin());
	}
	else{
		doc_count = 0;
	}
	
	cout<<card.card_name<<" "<<card.card_type<<" "<<"has just been returned"<<endl;
		doc_count++;
	
}
	else {
		deckOfCards.empty() ;
	
		throw isDeckEmpty();
	
	}
	
	return card;

}

int Deck::isDeckEmpty(){
	int isEmpty;
	try{
	if (deckOfCards.empty()){	
	isEmpty= deckOfCards.size();
	throw isEmpty;
	
	}
	}
		catch (int e){
		cout<<"Empty deckOfCards Exception in getCards"<<endl;	

		}
}

Advertisement
It is really hard to tell what is going on since the code contains many bugs.

For example, getCard has an over-complicated logic and fails to return a card (or throw an exception) in all cases (if doc_count >= deckOfCards.size()).

isDeckEmpty doesn't appear to do anything useful. It fails to answer the question whether deck is empty or not (doesn't return anything), and seems to be an abuse of exceptions just to print a simple message.

-------------
I suppose you want getCard to return a (random) card or throw an exception if there are no cards. It might be better to keep the logic as simple as possible. It is easiest to draw the top cards, because it is easier to erase from the back of the vector (with pop_back()).

Assuming the vector is already shuffled, pseudocode might look like this:

card drawCard():    if still_cards_left:        card = topmost_card        erase_topmost_card    else:        throw a nice descriptive exception (one found in <stdexcept> or derived from them    return card


If you chose to throw a std::runtime_error, for example, you'd catch the exception where you were calling it, and print the error description:

    try {        card = deck.getCard();        ...    }    catch (std::runtime_error& e) {        std::cout << e.what() << '\n';    }


Another thing is that running out of cards is not particularly exceptional, so you might try to build your code so that you never actually trigger the exception (might replace it with assert, as running out of cards would be a programmer's error, not a run-time exception). For example:

    while (deck.hasCards()) {        card = deck.getCard();        ...    }    //whatever happens next after running out of cards
I'm not a programming god, but it seems to me that your code is a mess and you probably don't need to be using exceptions at all. Or at least not in any way resembling how you currently use them.

Maybe you want something more like this:
pack::cardType Deck::getCard(){		resetCard(card); 	//deckOfCards is a vector containing  deck of cards	// check and make sure that the deck of cards are not empty.	if (!deckOfCards.empty()){		if (doc_count<deckOfCards.size()){			card = deckOfCards.at(doc_count);			deckOfCards.erase(deckOfCards.begin(),deckOfCards.begin());		}		else{			doc_count = 0;		}			cout<<card.card_name<<" "<<card.card_type<<" "<<"has just been returned"<<endl;				doc_count++;	}	else {		throw EMPTY_DECK;	}		return card;}then somewhere else...try{	myCard = getCard();}catch (int e){	if (e == EMPTY_DECK){		cout<<"Empty deckOfCards in getCards"<<endl;	}}


Edit: what visitor said, too :)
Thanks xDan and visitor. The code is a mess cos I'm still learning to use cplusplus exceptions.I know that this bit does not need to throw an exception but I wanted to use it as learning experience. Anyway, I tried your suggestions and it worked. I think I have a better understanding of how it works now. Thanks guys

This topic is closed to new replies.

Advertisement