Rethrowing the exception from the inner catch to the outer catch C++

Personal contribution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Program to explain rethrowing the exception
// from inner catch to outer catch

#include <iostream>
using namespace std;

void divide(double x, double y){

 cout << "\n Inside divide().";
 try {
  if( y == 0.0 ){ throw y;}
   else {cout << "\n Division : " << (x/y);}
}
 catch(double) { cout << "\n Caught double inside divide.";
  throw; 
}
 cout << "\n End of function.";
}

void main(){
 try { 
  cout << "\n Inside main().";
  divide(20.5, 10.5);
  divide(10.0, 0);
 }
 catch (double d){ cout << "\n Exception double inside main." <<;}
 cout << "\n END.";
}


 // Output displays here:



 // Output:

 // Inside main().
 // Inside divide().
 // Division : 1.95238
 // End of function.
 // Inside divide().
 // Caught double inside divide.
 // Exception double inside main.
 // END.

Topic archived. No new replies allowed.