1

i got the Maze class to generate a Random Maze. So the problem i face is that i want to throw some exceptions about the size of the maze ( i want to put a minimum), also if the number of columns is pair ( i neeed to make the maze symmetric). So i dont know how to handle three or 4 different exceptions and generate an error message

class Maze { 
  private: 

    int nr; // number of rows
    int nc; // number of columns
    int complex; // complexity of the maze: how hard to leave
    vector< vector<int> > vec; //array bidimensional de enteros
    //vector de dos dimensiones
    /*vector<T> elems;     // elements 
     // Create
    vector< vector<int> > vec(4, vector<int>(4));
    // Write
    vec[2][3] = 10;
    // Read
    int a = vec[2][3];*/
  public: 

    void Maze();
    void Maze( int rows, int columns, int hard);
    void PrintMaze();
    void ShowMaze(vec);
    bool isConnected ();
    void explore (Node posicion);
    bool validMove( Node actual);
    Node RandomNode(); // Obtienes un nodo al azar valido
    Node MirrorNode (Node actual); //devuelve el nodo espejo/mirror de un nodo null en otro caso..
    void reverseExplore();
    int getnr();
    int getnc();
    int getComplex();
    void setnr();
    void setnc();
    void setComplex();
    } 
}; 
void Maze::Maze(void) {
         cout <<"No hace naA"<<"\n\n";
}


void Maze::Maze(int rows, int columns, int hard) {

        //Comprobar numer filas y columnas es par/impar y sino salir dando mensaje correspondiente
    /*
    if (nr %2) != 0) {
        fprintf(stderr, "Error: El numero de filas tiene que ser par/impar.\n");
        exit();
        return 101;
    } else if (nc % 2 != 0) {
        fprintf(stderr, "Error: El numero de filas tiene que ser par/impar", nproc);
        return 102;
    }

    if (mr < 20 || nc < 10) {
        fprintf(stderr, "Error: El laberinto tiene que tener al menosu unas medidas\n");
        exit();
        return 103;
    }
*/
    nr=rows;
    nc=columns;
    hard=0;
    bool Final=false;
    Node actualNode;
    setupMaze();   //Inicializar laberinto ( todo a 0, poner los  bordes y casilla inicial)

    do {
        actualNode=randomNode();     //Obtengo un nodo aleatorio valido: que no sea pared y este en los limites
        addNode(actualNode); //Añado el nodo al laberinto
        if ( isConnected();) {
            //hard=0; //opcional para contar solo las veces que intenta en el  "mismo intento", ahora al colocar el nodo bien puedes inicializar los intentos a 0.
            cout << "\nInsercion correcta del nodo\n"; // compruebo si el laberinto es conexo o nodo
        }
        else {
            //Si laberinto resultante no es conexo, tengo que deshacer la inserción del nodoactual e incrementar el valor de hard
            deletenode( actualNode);
            hard++; 
        }
    while ( hard <= complex)

}

I need to make in C++ something like this in C. I would generate a maze with mymaze.maze( 40,80,10). I need to control this values in order to generate an symmetric maze and assure that the input variables have valids values ( number of rows have to be pair, maze has to has a minimum size of 20x10):

if (nr %2) != 0) {
            fprintf(stderr, "Error: El numero de filas tiene que ser par/impar.\n");
            exit();
            return 101;
        } else if (nc % 2 != 0) {
            fprintf(stderr, "Error: El numero de filas tiene que ser par/impar", nproc);
            return 102;
        }

        if (mr < 20 || nc < 10) {
            fprintf(stderr, "Error: El laberinto tiene que tener al menosu unas medidas\n");
            exit();
            return 103;
        }
    */

Thanks in advance

  • Check this stack overflow page, it should be useful: [link to similar question](http://stackoverflow.com/questions/12261915/howto-throw-stdexceptions-with-variable-messages) – Volodymyr Balytskyy Oct 10 '15 at 23:25
  • 1
    Whenever you would have written an error message in C, throw an exception instead. What happens when you try that? – Neil Kirk Oct 10 '15 at 23:25
  • 1
    Thanks but i dont know how to do it.. can anyone put some easy example of how to try and catch in c++ some of the examples ( ej: nc is pair number )...what you pointed out only throws the exception and dont know where they catch it.. – Eduardo Gutierrez Oct 11 '15 at 10:50
  • [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to the rescue. – Ivan Aksamentov - Drop Oct 11 '15 at 11:37

1 Answers1

0

Instead of fprintf() + exit() you can throw an exception.

throwing exceptions

Here is an example for throwing exceptions (see the Maze::Maze() implementation):

#include <iostream>
#include <stdexcept>

class Maze { 
public: 
    Maze();
    Maze(int rows, int columns, int hard);
private:
    int nr;
    int nc;
};

Maze::Maze(void) {
    std::cout << "No hace naA\n\n";
}

Maze::Maze(int rows, int columns, int hard) {

    if (rows % 2 != 0 || columns % 2 != 0)
        throw std::invalid_argument("Error: El numero de filas tiene que ser par/impar");

    if (rows < 20 || columns < 10) 
        throw std::out_of_range("Error: El laberinto tiene que tener al menosu unas medidas");

    // setup the maze ...
}

int main() {
    Maze m(41,80,10);  // 'invalid_argument' exception
    Maze m(2,2,10);    // 'out_of_range' exception
}

This program throws an invalid_argument exception, because rows is not even. The output is:

terminate called after throwing an instance of 'std::invalid_argument'
  what():  Error: El numero de filas tiene que ser par/impar

catching exceptions

Here is example for catching exceptions:

int main() {
    try {
        Maze m(41,80,10);
    } catch (const std::invalid_argument& e) {            
        std::cout << e.what() << std::endl;
        std::cout << "maze is not symetric" << std::endl;
    } catch (const  std::out_of_range& e) {
        std::cout << e.what() << std::endl;
        std::cout << "maze is too small" << std::endl;
    } catch (...) {
        std::cout << "some other exception" << std::endl;
    }
}

another approach (no exceptions)

Instead of throwing exceptions, you could "fix" incorrect user input:

Maze::Maze(int rows, int columns, int hard) {

    if (rows % 2 != 0)
        rows += 1;

    if (columns % 2 != 0)
        columns += 1;

    if (rows < 20)
        rows = 20;        

    if (columns < 10) 
        columns = 10;

    // setup the maze ...
 }
sergej
  • 17,147
  • 6
  • 52
  • 89
  • Many thanks for your help. Would i have enough throwing the exceptions without catching ? I dont like the solution of catching exceptions in the main function, as the main should only be used by customer or class user ( not by programmer or developer). Is there any option to catch the exception inside the constructor of the class ? Also where can i get more std errors as invalid argument and out of range ? – Eduardo Gutierrez Oct 11 '15 at 21:37
  • @EduardoGutierrez "throwing the exceptions without catching" is ok, the program will end if the user makes a wrong input. – sergej Oct 12 '15 at 07:13
  • @EduardoGutierrez Catching the exception inside the constructor does not make sense. You can fix incorrect input, see the update. – sergej Oct 12 '15 at 07:18
  • ok, i think its a more polite solution. I'll throw exceptions with message error to the user like: " Error: Number of columns have to be pair", so the program would stop and have a controlled termination. The user would have information about the error. Think its better solution. Dont you think so ? Many thanks for your help. – Eduardo Gutierrez Oct 12 '15 at 08:05
  • @EduardoGutierrez Yes, I think throwing an exception is a good solution in this case. – sergej Oct 12 '15 at 08:39