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()

M2103-TP4-Exo-2-Corrigé

 
/**
 *
 * \file   TestFailure.cpp
 *
 * \author D. Mathieu, M. Laporte
 *
 * \date   10/02/2011
 *
**/

#include <iostream>

#include "CstCodErr.h"
#include "CException.h"

using namespace std;
using namespace nsUtil;

namespace
{
    void testFailure (void)
    {
        cout << "testFailure\n\n";

        cin.exceptions (ios_base::failbit | ios_base::eofbit);
        try
        {
            int i;
            for (;;) 
            {
                cout << "Un entier : ";
                cin  >> i;
            }
        }
        catch (const ios_base::failure & exc)
        {
            if (cin.eof ())  cerr << "Fin de fichier\n";
            else if (cin.fail ()) cerr << "Erreur de lecture\n";
            cerr << exc.what () << '\n';

            throw;
        }
 
    } // testFailure()

} // namespace

int main (void)
{
    try
    {
        testFailure ();

        return KNoExc;
    }
    catch (const CException & e)
    {
        cerr << "Erreur        : " << e.getLibelle () << '\n' 
             << "Code d'erreur = " << e.getCodErr ()  << '\n';
        return e.getCodErr ();
    }    
    catch (const exception & e)
    {
        cerr << "Exception standard : " << e.what () << '\n'; 
        return KExcStd;
    }    
    catch (...)
    {
        cerr << "Exception inconnue\n";
        return KExcInconnue;
    }    

} // main()

M2103-TP4-Exo-3-Corrigé

 
/**
 *
 * \file    CstCodErr.h
 *
 * \authors M. Laporte, D. Mathieu
 *
 * \date    10/02/2011
 *
 * \version V1.0
 *
 * \brief   Codes d'erreurs
 *
 **/
#ifndef __CSTCODERR_H__
#define __CSTCODERR_H__

namespace nsUtil
{
    enum {KNoExc       = 0,
          KNoError     = 0,
          
          KExcDivZero  = 11,  // Division par zero

          KExcStd      = 254,
          KExcInconnue = 255 
         };

} // namespace nsUtil

#endif /* __CSTCODERR_H__ */

 
 
/**
 *
 * \file   DivisionParZero.cxx
 *
 * \author D. Mathieu
 *
 * \date   07/12/2011
 *
**/
#include <iostream>
#include <exception>
#include <iomanip>      // setw()

#include "CstCodErr.h"
#include "CException.h"

using namespace std;
using namespace nsUtil;

namespace
{
    int divisionEntiere (int num, int denom) throw (CException)
    {
        if (0 == denom) 
            throw CException ("Division par zero", KExcDivZero);
        return num / denom;

    } // divisionEntiere()
                                
    void divisionParZero ()
    {
        int lesNums   []               = { 12,  3, -5,  0, 40 };

        const unsigned KSzFractions = 
            sizeof (lesNums) / sizeof (lesNums [0]);

        int lesDenoms [KSzFractions] = {  4,  0, -5, 10,  4 };

        for (unsigned i = 0; i < KSzFractions; ++i)
        {
            cout << setw (4) << lesNums [i]   << " / "
                 << setw (4) << lesDenoms [i] << " = "; 
            try 
            {
                cout << divisionEntiere (lesNums [i], lesDenoms [i])
                     << '\n';
            }
            catch (const CException & e)
            {
                cout << "Erreur : " << e.getLibelle() 
                     << "; Code d'erreur = " << e.getCodErr() << '\n';
            }
        }

    } // divisionParZero()

} // namespace

int main ()
{
    try
    {
        divisionParZero();

        return KNoExc;
    }
    catch (const CException & e)
    {
        cerr << "Erreur        : " << e.getLibelle() << '\n' 
             << "Code d'erreur = " << e.getCodErr() << '\n';
        return e.getCodErr();
    }    
    catch (const exception & e)
    {
        cerr << "Exception standard : " << e.what() << '\n'; 
        return KExcStd;
    }    
    catch (...)
    {
        cerr << "Exception inconnue\n";
        return KExcInconnue;
    }    

} // main()

M2103-TP4-Exo-4-Corrigé

 
/**
 *
 * \file    Rationnel.h
 *
 * \authors M. Laporte, D. Mathieu
 *
 * \date    06/11/2007
 *
 * \version V1.0
 *
 * \brief   Declaration de la classe Rationnel - V2
 *             Gestion des exceptions
 *
 **/
#ifndef __RATIONNEL_H__
#define __RATIONNEL_H__

#include "CException.h"

typedef unsigned long long ULLong_t;

namespace nsMath
{
    class Rationnel 
    {
        int myNum;
        int myDenom;
 
        void Simplifier (void)                                 noexcept;

      public : 
        Rationnel (const int num = 0, const int denom = 1) 
                                             throw (nsUtil::CException);
        Rationnel (const Rationnel & r)                      noexcept;

        void display (void) const;

        bool operator <       (const Rationnel & r)  const    noexcept;
        bool operator ==      (const Rationnel & r)  const    noexcept;

        Rationnel operator + (const Rationnel & r)  const    noexcept;
        Rationnel operator - (const Rationnel & r)  const    noexcept;
        Rationnel operator * (const Rationnel & r)  const    noexcept;
        Rationnel operator / (const Rationnel & r)  const
                                             throw (nsUtil::CException);

    }; // Rationnel 
    
} // namespace nsMath

#endif /* __RATIONNEL_H__ */
 
/**
 *
 * \file    Rationnel.cpp
 *
 * \authors M. Laporte, D. Mathieu
 *
 * \date    07/12/2011
 *
 * \version V1.0
 *
 * \brief   Definition des methodes de la classe Rationnel  (V2)
 *             Gestion des exceptions
 *
 **/
#include <iostream>
#include <exception>
#include  <cmath>   // abs()

#include "Rationnel.h"
#include "CstCodErr.h"
#include "CException.h"

#define RATIONNEL nsMath::Rationnel

using namespace std;
using namespace nsUtil;
using namespace nsMath;

namespace
{
    unsigned PGDC (unsigned a, unsigned b) noexcept
    {
        for ( ; a != b; )
        {
            if (a < b)
                b -= a;
            else
                a -= b;
        }
        return a;

    } // PGDC()

} // namespace

RATIONNEL::Rationnel (const int num   /* = 0 */, 
                        const int denom /* = 1 */) throw (CException)
    : myNum (num), myDenom (denom)
{
    if (myDenom == 0) throw CException ("Diviseur nul", KExcDivZero);
    Simplifier ();

} // Rationnel()

RATIONNEL::Rationnel (const Rationnel & r) noexcept
    : myNum (r.myNum), myDenom (r.myDenom) {}

void RATIONNEL::display (void) const
{
    cout << myNum << '/' << myDenom;

} // display()

void RATIONNEL::Simplifier (void) noexcept
{
    if (myDenom < 0)
    {
        myNum   = -myNum;
        myDenom = -myDenom;
    }
    int pgdc = (myNum == 0) ? myDenom 
                            : PGDC (abs (myNum), abs (myDenom));

    myNum   /= pgdc;
    myDenom /= pgdc;

} // Simplifier() 

bool RATIONNEL::operator < (const Rationnel & r) const noexcept
{
    return myNum * r.myDenom < myDenom * r.myNum;

} // operator <

bool RATIONNEL::operator == (const Rationnel & r) const noexcept
{
    return myNum == r.myNum && myDenom == r.myDenom;

} // operator ==

RATIONNEL RATIONNEL::operator + (const Rationnel & r) 
    const noexcept
{
    return Rationnel (myNum   * r.myDenom + r.myNum * myDenom, 
                       myDenom * r.myDenom); 

} // operator +

RATIONNEL RATIONNEL::operator - (const Rationnel & r) 
    const noexcept
{
    return Rationnel (myNum   * r.myDenom - r.myNum * myDenom, 
                       myDenom * r.myDenom); 

} // operator -

RATIONNEL RATIONNEL::operator * (const Rationnel & r) 
    const noexcept
{
    return Rationnel (myNum   * r.myNum, 
                       myDenom * r.myDenom); 

} // operator *

RATIONNEL RATIONNEL::operator / (const Rationnel & r) 
    const throw (CException) 
{
    if (r.myNum == 0) 
        throw CException ("Division par zero", KExcDivZero);
    return Rationnel (myNum * r.myDenom, myDenom * r.myNum); 

} // operator /

#undef CRATIONNEL

 
/**
 *
 * \file     TestRationnel.cxx
 *
 * \authors  M. Laporte, D. Mathieu
 *
 * \date      07/12/2011
 *
 * \version  V1.0
 *
 * \brief    Test de la classe Rationnel (V2)
 *              Gestion des exceptions
 *
 **/
#include <iostream>
#include <exception>

#include "Rationnel.h"

using namespace std;

using namespace nsUtil;
using namespace nsMath;

namespace 
{
    void testRationnel (void)
    {
        try
        {
            cout << "Rationnel R1 (12, 0)\n";
            Rationnel r1 (12, 0);
            cout << "R1 = ";
            r1.display ();
            cout << '\n';
        }
        catch (const CException & e)
        {
            cout << "Erreur : " << e.getLibelle ()
                 << "; code d'erreur = " << e.getCodErr () << '\n';
        }
        try
        {
            Rationnel r1 (4, 12);
            cout << "R1 = ";
            r1.display ();
            cout << '\n';

            Rationnel r2 (0, 12);
            cout << "R2 = ";
            r2.display ();
            cout << '\n';

            r1.display();
            cout << " / ";
            r2.display();
            cout << " = ";
            (r1 / r2).display ();
            cout << '\n';
        }
        catch (const CException & e)
        {
            cout << "Erreur : " << e.getLibelle ()
                 << "; code d'erreur = " << e.getCodErr () << '\n';
        }

    } // testRationnel()

} // namespace

int main (void)
{
    try
    {
        testRationnel ();
    }
    catch (const CException & e)
    {
        cerr << "Erreur        : " << e.getLibelle () << '\n'
             << "Code d'erreur = " << e.getCodErr  () << '\n';
        return e.getCodErr();
    }
    catch (const exception & e)
    {
        cerr << "Exception standard : " << e.what () << '\n';
        return KExcStd;
    }
    catch (...)
    {
        cerr << "Exception inconnue\n";
        return KExcInconnue;
    }
    return KNoExc;

} // main()