Hello programmers!

I am working on a Tic-Tac-Toe player vs. computer class, which is supposed to have a run.() method in order for the game to start. In a gameWon() member function, I am checking for a win, either by the player or by the computer. I check to see if a row was won, if a column was won, and if a diagonal was won. With each scenario, a private member function is called that returns a bool. In my diagonalWon() function, I check to see if I have an equal number of rows and columns, which means that there are two diagonals avaliable. However, if the number of rows doesn't equal the number of columns, I want an error to be raised that terminates the function execution. My code is below:

bool TicTacToe::diagonalWon()
    //checks to se if player won using the diagonals
        //check to see if there are diagonals
        if (rows!=columns)
            //there are no diagonals. throw exception #1
            throw 1;
            //continue to check if the diagonals are won
        cerr << "\n\nERROR in diagonalWon"<<endl;
        return nullptr;

However, I get an xCode message that the control may reach the end of a non-void function. How can I successfuly raise the error, which in reply to exception #1 prints: There are no diagonals, while eliminating the message successfully?

Recommended Answers

All 8 Replies

Your function says it returns a bool. It doesn't. It should.

The comments in the if-else decision structure will return the bool. I'm just worried about the exception part. The "meat" of the program will come later.

That is- the //continue to check if the diagonals are won will be replaced with code that returns a bool.

You throw an integer, so your catcher should catch an integer - catch (int theThrownException)

That return nullptr in your catch block doesn't make a lot of sense.

Is there any way programatically that you can fix the issue that there are not the same number of rows as columns? If so, fix it and carry on. If not, all you can do is dump an appropriate message to the user and terminate the program.

If the exception handler here can fix everything, then you should be able to carry on and return a sensible boolean. If the exception handler here cannot fix everything, then you need to kick the exception up the stack (i.e. throw it) to the level that can fix everything (and if it's unfixable, a sensible error message and termination).

Can you give me some sample code as to how to dump a message and terminate the program using what I have above? I don't know how to fix this.

You can just throw the exception again. Eventually, when nothing caches it, the program will terminate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.