M2103-TP4-Exo-1-Corrigé

namespace
{
    void clearScreen (void)
    {
         cout << "\033[2J\033[1;1H" << flush;

    } // clearScreen()

    char choixDansMenu (void)
    {
        clearScreen();

        cout << "A : exception 'exception'\n"
                "B : exception standard specifique\n"
                "C : exception 'CException'\n"
                "D : exception inconnue\n\n"
                "Votre choix (suivi de ) : ";

        char choix;
        cin >> choix;

        clearScreen();

        return choix;

    } // ChoixDansMenu()

    void traiterCommande (char cmd)
    {
        switch (cmd)
        {
          case 'A' :
          case 'a' :
            throw exception ();

          case 'B' :
          case 'b' :
            {
              // throw runtime_error ("erreur d'execution ...");
              string s;
              cout << s.at (0);
              break;     // inutile puisqu'une exception est levee avant
            }

          case 'C' :
          case 'c' :
            throw CException ("Surprise, surprise !", 123);

          case 'D' :
          case 'd' :
            throw 123;
        }

    } // traiterCommande()

    void testExceptionsInMain ()
    {
         for ( ; ; ) traiterCommande (choixDansMenu());

    } // testExceptionsInMain()

} // namespace

int main ()
{
    try
    {
        testExceptionsInMain ();

        return KNoExc;
    }
    catch (const CException & e)
    {
        cerr << "Erreur        : " << e.getLibelle () << '\n' 
             << "Code d'erreur = " << e.getCodErr ()  << '\n';
        return e.getCodErr();
    }    
    catch (const out_of_range & e)    // levee par string::at()
    {
        cerr << "Exception out_of_range : " << e.what () << '\n'; 
        return KExcStd;
    } 
    */   
    /*
    catch (const runtime_error & e)
    {
        cerr << "Exception runtime_error : " << e.what () << '\n'; 
        return KExcStd;
    } 
    */   
    catch (const exception & e)
    {
        cerr << "Exception standard : " << e.what () << '\n'; 
        return KExcStd;
    }    
    /*
    catch (const unsigned & e)
    {
        cerr << "Exception unsigned : " << e << '\n'; 
        return KExcStd;
    }    
    catch (const int & e)
    {
        cerr << "Exception int : " << e << '\n'; 
        return KExcStd;
    }
    */   
    catch (...)
    {
        cerr << "Exception inconnue\n";
        return KExcInconnue;
    }    

} // main()