zurück weiter

Ausnahmebehandlung
Beispiel 3

G.Eichelsdörfer
Staatliche Technikerschule Weilburg

Der Ausnahme-Informations-Typ ist hier ein String.

#include <conio.h>
#include <iostream.h>

double Kehrwert(double x)
{
 if (x==0.0throw "Ausnahme in \"Kehrwert\": Division durch Null.";
 return 1/x;
}

int main()
{double x, y;
 cout << "Dritte Demonstration einer Ausnahmebehandlung\n" << endl
  << "Hier wird der Kehrwert einer eingegebenen Fliesskommazahl berechnet." << endl
  << "Bei Eingabe einer Null wird eine Exception ausgeloest.\n" << endl;
 try
 {
  while(1)
  {
   cout << "Geben Sie eine Fliesskommazahl ein: ";
   cin >> x;
   y = Kehrwert(x);
   cout << "Der Kehrwert von " << x << " ist " << y << ".\n" << endl;
  }
 }
 catch(char *str) // Ausnahmen mit Informations-String
 {
  cout << endl << str << endl;
 }
 getch();
 return 0;
}