try/catch

deiwid

Grünschnabel
hallo

hab eine frage bezüglich try und catch....wie funktioniert das Kann mir das bitte jemand erklären, wann das programm wohin springt. Vielen Dank.

PHP:
#include<iostream>
using namespace std;

 int main() {
   try {
      for (int i=0; i<7;i=i+1) {
         if ((i%3)==2) throw i;
         if (i%5) throw double(i);
         cout<<i;
      }   
   }
   catch(int err) {cout<<err/2;}
   catch(double err) {cout<<err/2;}
   catch(...) {cout<<"Ooooops";}
   return 0;
 }

 Gesucht ist die Ausgabe
 
Hi.

Es gibt doch aber nur 2 Stellen innerhalb des try Blocks wo etwas geworfen wird. Geh doch einfach mal schrittweise durch das Programm. Was passiert denn da? Das Programm ist nicht kompiliziert, eine ganz einfache Schleife: was wird ausgeführt für i = 0 ? für i = 1 ?

Gruß
 
Zurück