Exceptions

messier79 Messages postés 10 Date d'inscription samedi 24 janvier 2004 Statut Membre Dernière intervention 23 avril 2004 - 22 avril 2004 à 23:37
ymca2003 Messages postés 2070 Date d'inscription mardi 22 avril 2003 Statut Membre Dernière intervention 3 juillet 2006 - 23 avril 2004 à 23:31
Bonjour

J'essaie de definir mes propres exceptions. Pour celà, j'ai un fichier .h :

Code:
#ifndef EXCEPTION_H
#define EXCEPTION_H

#include <string>
#include <conio.h>

using namespace std;

class StandardException : public exception
{
protected :
string msg;

public:
StandardException() throw();
StandardException(const exception&) throw();
StandardException& operator=(const StandardException&) throw();
StandardException(string msgerr) throw();
virtual ~StandardException() throw();
virtual const char* what(void) const throw();
};

class InFileException : public StandardException
{
public:
InFileException(string);
};

class OutFileException : public StandardException
{
public:
OutFileException(string);
};

class MenuChoiceException : public StandardException
{
public:
MenuChoiceException(string);
};

#endif

Avec le fichier .cpp associe :

Code:
#include <string>
#include <conio.h>
#include "Exceptions.h"

using namespace std;

StandardException::StandardException()
{
// msg="\nUne Exception a ete soulevee.\n";
}

StandardException::StandardException(string msgerr)
{
msg=msgerr;
}

string StandardException::what()
{
return(msg);
}

InFileException::InFileException(string msgerr)
{
msg="Une erreur s'est produite a l'ouverture du fichier de donnees.\n";
msg+="Verifiez l'existence des fichiers passes en parametre et reessayez.\n";
msg+="Si le probleme persiste, contactez l'Administrateur.\n\n";
}

...

A la compilation, j'obtiens :
Code:

Exceptions.cpp:8: declaration of `StandardException::StandardException()'
throws different exceptions
Exceptions.h:15: than previous declaration `
StandardException::StandardException() throw ()'

Exceptions.cpp:13: declaration of `
StandardException::StandardException(std::basic_string<char,
std::char_traits<char>, std::allocator<char> >)' throws different exceptions
Exceptions.h:18: than previous declaration `
StandardException::StandardException(std::basic_string<char,
std::char_traits<char>, std::allocator<char> >) throw ()'

Exceptions.cpp:18: prototype for `std::string StandardException::what()' does

not match any in class `StandardException'
Exceptions.h:20: candidate is: virtual const char* StandardException::what()
const

Que manque-t-il ?

Merci

3 réponses

ymca2003 Messages postés 2070 Date d'inscription mardi 22 avril 2003 Statut Membre Dernière intervention 3 juillet 2006 7
23 avril 2004 à 01:06
essaie de rajouter throw() également dans la définition des fct :

StandardException::StandardException() throw()
{
// msg="\nUne Exception a ete soulevee.\n";
}
0
messier79 Messages postés 10 Date d'inscription samedi 24 janvier 2004 Statut Membre Dernière intervention 23 avril 2004
23 avril 2004 à 01:23
Ca marche mieux, mais maintenant, j'ai une erreur pour le destructeur :

Exceptions.h:10: looser throw specifier for `virtual

StandardException::~StandardException()'
0
ymca2003 Messages postés 2070 Date d'inscription mardi 22 avril 2003 Statut Membre Dernière intervention 3 juillet 2006 7
23 avril 2004 à 23:31
il ne veut peut-être pas de la clause Throw() pour un destructeur.
D'autre part, je ne vois pas à quoi servent ces clauses dans des fct vu qu'aucune d'entre elles ne génère une exception.
0
Rejoignez-nous