Multiple catch block vs generic catch block

Mar 14, 2017 at 11:01pm
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 // Multiple catch block:
  
// Program to explain how to use multiple catch blocks.

#include <iostream>
using namespace std;


void test(int x){
   try{
       
      if(x==0){ throw 'B'; }
      else if(x == 1){ throw x; }
      else if(x == 2){ throw 1.0;}
      else if(x == 3){ throw "Hello";}
      
      cout << "\nEnd of try-block.";
   }
      catch(char c){ cout << "caught a char: " << c; }
      catch(int i){ cout << "caught an int: " << i; }
      catch(double d){ cout << "caught a double: " << d;}
      catch(const char* s){ cout << s;}
   
   cout << "\nEnd of try-catch system. \n\n";
}


int main(){

cout << "\nTesting Multiple catches: \n" << endl << endl;

cout << "x == 0\n";
test(0);

cout << "x == 1\n";
test(1);

cout << "x == 2\n";
test(2);

cout << "x == 3\n";
test(3);

cout << "x == 4\n";
test(4);

cout << "END. \n";

   return 0;
}




Testing Multiple catches: 


x == 0
caught a char: B
End of try-catch system. 

x == 1
caught an int: 1
End of try-catch system. 

x == 2
caught a double: 1
End of try-catch system. 

x == 3
Hello
End of try-catch system. 

x == 4

End of try-block.
End of try-catch system. 

END. 


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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Generic catch block:

// Program to explain how to use multiple catch blocks.

#include <iostream>
using namespace std;


void test(int x){
   try{
      if(x==0){ throw 'B'; }
      else if(x == 1){ throw x; }
      else if(x == 2){ throw 1.0;}
      else if(x == 3){ throw "Hello";}
      cout << "\nEnd of try-block.\n";
   }
      catch(...){
         cout << "Exception caught!\n";
      }
      cout << "End of try-catch block.\n\n";
}


int main(){

cout << "\nTesting Multiple catches: \n" << endl << endl;

cout << "x == 0\n";
test(0);

cout << "x == 1\n";
test(1);

cout << "x == 23\n";
test(2);

cout << "x == 3\n";
test(3);

cout << "x == 4\n";
test(4);

cout << "END. \n";

   return 0;
}




Testing Multiple catches: 


x == 0
Exception caught!
End of try-catch block.

x == 1
Exception caught!
End of try-catch block.

x == 23
Exception caught!
End of try-catch block.

x == 3
Exception caught!
End of try-catch block.

x == 4

End of try-block.
End of try-catch block.

END. 
Last edited on Mar 14, 2017 at 11:01pm
Mar 16, 2017 at 4:43pm
Hello Kourosh23,

Thank you for the input,

Andy
Mar 16, 2017 at 4:56pm
Except that there is not a lot of point in placing the trythrow and catch in the same scope. The test function can throw, so enclose the function call (in main) in a try block. The catch would also have to be in main in that case, as it it is main which is handling the exception.

Last edited on Mar 21, 2017 at 1:46am
Mar 19, 2017 at 10:19pm
@Handy Andy any time man.

@TheIdeasMan, I see, so you mean that I have to write all my try inside other functions for example void, and then have my catch inside main ?

I guess the point is in throw;, when it happens, catch-block will catch the exception inside main().
Last edited on Mar 19, 2017 at 10:20pm
Mar 21, 2017 at 1:46am
I guess the point is in throw;, when it happens, catch-block will catch the exception inside main().


Yes, I worded my reply incorrectly.

It is pointless to have the throw and catch in the same scope.
Topic archived. No new replies allowed.