1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <iostream>
#include <string>
#include <cassert>
char premiereLettre(const std::string& nom)
{
assert(nom.length() > 0); // en Debug, arrête le programme si nom.length() == 0
return nom.at(0); // lance une exception std::out_of_range si nom.length() == 0
}
int main() {
try {
std::string nom;
char c = premiereLettre(nom);
std::cout << "Premiere lettre : '" << c << "'.";
} catch(std::logic_error& e) {
std::cerr << "Erreur de programmation : " << e.what();
} catch(std::exception& e) {
std::cerr << "Erreur : " << e.what();
}
return 0;
} |