3 Replies - 541 Views - Last Post: 19 December 2009 - 05:34 PM Rate Topic: -----

#1 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

C++ object, how do I avoid this?

Posted 19 December 2009 - 09:01 AM

Card getCard(UINT a)
	{
		try{
			return deckOne.at(deckIndex.at(a));
		}
		catch(std::exception &e)
		{
			//fuuuuu
		}
	}

I have two vectors, one filled with Card objects, another with indices. The problem I am having is returning a value on error..?
I haven't done a lot of OOP yet. This method is inside a class, what's a good way to fix this error? Make a default card to return? How do I denote an error with the return value? I could put a reference to a bool or int in the paramter list which will tell me if an error has occured, so what should I return?

Is This A Good Question/Topic? 0
  • +

Replies To: C++ object, how do I avoid this?

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: C++ object, how do I avoid this?

Posted 19 December 2009 - 10:15 AM

I guess its up to you what you denote as an error as far as a return value goes. For instance, you could just return false when an error occures. Like this

bool func()
{
	if (there is a problem)
		return false; 
	else 
		return true;
}




Then just test the function like this

if (!func())
	there was a problem in the function, take appropriate action


if you wish to have multiple possible errors, use a value and maybe define these errors

#define ERROR_1 -1
#define ERROR_2 -2

etc...


Was This Post Helpful? 0
  • +
  • -

#3 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: C++ object, how do I avoid this?

Posted 19 December 2009 - 10:56 AM

Well, what you can do, is just throw the exception, if one occurs. Then, when you call the method, you can try/catch that method itself and check for exceptions.

// unsigned int, as opposed to UINT?
Card getCard(const unsigned int a)
{
    try
    {
        return deckOne.at(deckIndex.at(a));
    }
    catch (std::exception &e)
    {
        throw e;
    }
}

// when you call it:

try
{
    // however you'd call it.
}
catch (std::exception e)
{
    // handle the error however you'd like.
}



Hope this helps! :)

This post has been edited by Locke: 20 December 2009 - 05:55 PM

Was This Post Helpful? 1
  • +
  • -

#4 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: C++ object, how do I avoid this?

Posted 19 December 2009 - 05:34 PM

From my understanding a language like ADA features things like: continue execution even in the event of an error
I don't like throwing an exeption that brings up a message telling me to contact the application's support team. I *am* the application's support team. It looks like it will take some work to make it continue execution. BUT, thanks for all the help! You really did help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1