M2103-TP6-Exo-2-Corrigé

 
/**
 *
 * @file    CException.h
 *
 * @authors M. Laporte, D. Mathieu
 *
 * @date    23/03/2010
 *
 * @version V1.0
 *
 * @brief  classe CException
 *
 **/
#ifndef __CEXCEPTION_H__
#define __CEXCEPTION_H__

#include <string>
#include <iostream>
#include <exception>

#include "CstCodErr.h"
#include "CEditable.h"

namespace nsUtil
{
    class CException : public std::exception, public IEditable
    {
        std::string myLibelle;
        unsigned    myCodErr;

      public :
        CException (const std::string & libelle = std::string(), 
                    const unsigned      codErr  = KNoExc)     noexcept;
        virtual ~CException (void)                            noexcept;

        const std::string & getLibelle (void) const           noexcept;
        unsigned            getCodErr  (void) const           noexcept;

        virtual const char* what       (void) const           noexcept;

      protected :
        virtual std::ostream & display (std::ostream & os) const;

    }; // CException
    
} // namespace nsUtil

#endif /* __CEXCEPTION_H__ */
 
/**
 *
 * @file     CException.cpp
 *
 * @authors  M. Laporte, D. Mathieu
 *
 * @date     02/04/2010
 *
 * @version  V1.0
 *
 * @brief    classe CException
 *
 **/
#include <string>
#include <iostream>

#include "CstCodErr.h"
#include "IEditable.hpp"

#include "CException.h"

using namespace std;

ostream & nsUtil::CException::display (ostream & os) const
{ 
    return os << "Exception : " << myLibelle << '\n'
              << "Code      : " << myCodErr;

} // display()


#define CEXC nsUtil::CException

CEXC::CException (const std::string & libelle /* = std::string () */,
                  const unsigned      codErr  /* = KNoExc       */) 
                                                               noexcept 
    : myLibelle (libelle), myCodErr (codErr) {}

const std::string & CEXC::getLibelle (void) const noexcept 
{
    return myLibelle;

} // getLibelle()

unsigned CEXC::getCodErr (void) const noexcept { return myCodErr;  }

CEXC::~CException (void) noexcept {}

const char* CEXC::what() const noexcept  { return myLibelle.c_str(); }

#undef CEXC

 
/**
 *
 * @file    TestCException.cpp
 *
 * @authors D. Mathieu
 *
 * @date    17/03/2010
 *
 * @version V2.0
 *
 * @brief   Test de la classe CException
 *
 **/
#include <iostream>
#include <string>
#include <esception>

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

using namespace std;
using namespace nsUtil;    // CException

namespace 
{
    class ExcFille : public CException
    {
      public :
        ExcFille (const string & libel, unsigned val) throw ()
          : CException (libel, val) {}

      protected :
        virtual ostream & display (ostream & os) const
        {
            return CException::display (os) << " de la classe fille";

        } // display()

    }; // CFille

    void testCException (void)
    {
        throw ExcFille ("Test du polymorphisme", 25);

    }// testCException ()

} // namespace

int main (int argc, char * argv [])
{
    if (argc != 1)
    {
        cerr << "Nombre d'arguments invalide\n"
                "Usage : ./TestCException\n";
        return KExcArg;
    }
    try
    {
        testCException();

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

} // main()