// exception.cpp Création et utilisation d'exceptions
// > deux CLASSES d'EXCEPTION
// > exemple de lancement d'une exception de type double
// > exemple de capture d'une exception de type double
// auteur: R.Astier
//
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
#include
#include
#include
using namespace std;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
class ExcFichier {
public:
ExcFichier(int e) : etat(e) { }
string message();
protected:
int etat;
string msg;
};
// Erreur sur fichier en lecture (entrée)
class ExcFichierE : ExcFichier {
public:
string message() {return msg; };
ExcFichierE(char * txt, int e): ExcFichier(e) {
msg += "* * (ExcFichierE) Fichier ";
(msg += txt) += " non accessible en lecture";
}
};
// Erreur sur fichier en sortie ou écriture
class ExcFichierS : ExcFichier {
public:
string message() {return msg; } ;
ExcFichierS(char * txt, int e): ExcFichier(e) {
msg += "* * (ExcFichierE) Fichier '";
(msg += txt) += "' ne peut pas être créé";
}
private:
int etat;
};
string lireFic(char nomFic[]) throw (ExcFichierE) {
ifstream fic(nomFic,ios::ate);
if(!fic) throw ExcFichierE(nomFic, fic.rdstate());
long taille=fic.tellg();
cout << "taille de "<< nomFic << ": "<>dist;
cout<<"durée ? "; cin>>duree;
try {
cout<< "Vitesse "<< vitesse(dist,duree)< "<< vitesse(3.14,0)< longueur: " << txt.length() << endl;
ecrireFic(txt,"toto");
}
catch(ExcFichierE e) { cout<