Search code examples
c++constructorexceptionmaze

Constructor c++ and handling exception


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


Solution

  • 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 ...
     }