C++ Exception Handling: Umfassender Leitfaden
- throw – Programm wirft Exception bei Problem (z. B. Division durch Null).
- catch – Exception-Handler an der Stelle, wo Problem behandelt werden soll.
- try – Block, für den bestimmte Exceptions aktiviert sind. Gefolgt von einem/mehreren
catch-Blöcken.
Syntax: try/catch
try {
// geschützter Code
} catch(ExceptionName1 e1) {
// Handler für ExceptionName1
} catch(ExceptionName2 e2) {
// Handler für ExceptionName2
} catch(ExceptionNameN eN) {
// Handler für ExceptionNameN
}
Mehrere catch für verschiedene Exception-Typen im try-Block möglich.
Exceptions werfen
throw an beliebiger Stelle im Codeblock. Operand bestimmt Exception-Typ.
double division(int a, int b) {
if(b == 0) throw "Division by zero condition!";
return (a/b);
}
Exceptions fangen
catch nach try fängt jede Exception. Typ in Klammern nach catch spezifizieren:
try { ... } catch(ExceptionName e) { ... }
Für beliebigen Exception-Typ: Ellipsis ...:
try { ... } catch(...) { ... }
Beispiel: Division durch Null
#include <iostream>
using namespace std;
double division(int a, int b) {
if(b == 0) throw "Division by zero condition!";
return (a/b);
}
int main() {
int x = 50, y = 0; double z = 0;
try {
z = division(x, y);
cout << z << endl;
} catch(const char* msg) {
cerr << msg << endl;
}
return 0;
}
Ausgabe: Division by zero condition!
C++ Standard-Exceptions (<exception>)
Hierarchie (Eltern → Kind):
| Nr. | Exception & Beschreibung |
|---|---|
| 1 | std::exception – Basisklasse aller Standard-Exceptions |
| 2 | std::bad_alloc – von new geworfen |
| 3 | std::bad_cast – von dynamic_cast |
| 4 | std::bad_exception – für unerwartete Exceptions |
| 5 | std::bad_typeid – von typeid |
| 6 | std::logic_error – theoretisch durch Code-Lesen erkennbar |
| 7 | std::domain_error – mathematisch ungültiger Definitionsbereich |
| 8 | std::invalid_argument – ungültige Argumente |
| 9 | std::length_error – zu großer string |
| 10 | std::out_of_range – z. B. vector::at(), bitset::operator[] |
| 11 | std::runtime_error – theoretisch NICHT durch Code-Lesen erkennbar |
| 12 | std::overflow_error – mathematischer Überlauf |
| 13 | std::range_error – Wert außerhalb Range |
| 14 | std::underflow_error – mathematischer Unterlauf |
Eigene Exceptions definieren
Vererbung von std::exception + Überschreiben von what():
#include <iostream>
#include <exception>
using namespace std;
struct MyException : public exception {
const char* what() const throw() { return "C++ Exception"; }
};
int main() {
try { throw MyException(); }
catch(MyException& e) {
cout << "MyException caught" << endl;
cout << e.what() << endl;
} catch(std::exception& e) { /* andere */ }
return 0;
}
Ausgabe:
MyException caught
C++ Exception
what() ist öffentliche Methode von exception, von allen Kind-Exceptions überschrieben – liefert Fehlerursache.
C Sprache
- C++-Operatoren meistern: Ein vollständiger Leitfaden mit praktischen Beispielen
- C++-Kommentare: Best Practices für lesbaren, wartbaren Code
- C++ Klassentemplates meistern: Ein praktischer Leitfaden
- C-Dateiverwaltung: I/O mit fopen, fprintf, fread & fseek meistern
- Java Exception Handling meistern: Try, Catch, Finally, Throw & Throws erklärt
- C++ Exception Handling meistern: Praktische try, catch, throw Beispiele
- Python Exception Handling meistern: try, except, finally und raise effektiv nutzen
- Fehlerbehandlung in C meistern: Best Practices & Techniken
- C++ Programmiersprache: Überblick & Kernfeatures
- C++ Konstanten & Literale: Typen, Syntax & Best Practices