Lesson of 24 February 2011 (part B)¶
Using exceptions
An example of using exceptions
File exception1.cc
1#include <iostream>
2#include <cmath>
3#include <string>
4#include <sstream>
5#include <stdexcept>
6
7using namespace std ;
8
9/*
10 * A simple example of exception handling
11 *
12 */
13
14class my_throw {
15public:
16 string where ;
17 my_throw( string const & file, unsigned line ) {
18 stringstream msg ;
19 msg << "Found an error in file: " << file << " at line: " << line ;
20 where = msg . str() ;
21 }
22} ;
23
24#define MY_THROW throw my_throw( __FILE__, __LINE__ )
25
26// declare prototype
27void a() ;
28void b() ;
29void c() ;
30
31void
32a() {
33 cout << "Enter in routine a\n" ;
34 b() ;
35 cout << "Exit from routine a\n" ;
36} ;
37
38void
39b() {
40 cout << "Enter in routine b\n" ;
41 c() ;
42 cout << "Exit from routine b\n" ;
43} ;
44
45void
46c() {
47 cout << "Enter in routine c\n" ;
48 // make an exception
49 throw logic_error("pluto") ;
50 throw runtime_error("pippo") ;
51 MY_THROW ;
52 throw "I am and excption string" ;
53 cout << "Exit from routine c\n" ;
54} ;
55
56int
57main() {
58 try {
59 try {
60 a() ;
61 }
62 catch (...) {
63 cout << "nested exception\n" ;
64 throw ; // I rethrow the same exception to the upper block
65 }
66 a() ;
67 }
68 catch ( int e ) {
69 cout << "found an exception!\n" ;
70 cout << "value = " << e << "\n" ;
71 }
72 catch ( char const str [] ) {
73 cout << "found an exception!\n" ;
74 cout << "string = " << str << "\n" ;
75 }
76 catch ( my_throw const & e ) {
77 cout << e . where << '\n' ;
78 }
79 catch ( runtime_error const & e ) {
80 cout << "runtime_error: " << e . what() << '\n' ;
81 }
82 catch ( exception const & e ) {
83 cout << "exception: " << e . what() << '\n' ;
84 }
85 catch ( ... ) {
86 cout << "found an exception!\n" ;
87 }
88 return 0 ;
89}
A second example
File exception2.cc
1#include <iostream>
2#include <cmath>
3#include <string>
4#include <sstream>
5#include <stdexcept>
6#include <memory>
7
8using namespace std ;
9
10/*
11 * A simple stupid class
12 */
13
14class stupid {
15 string my_name ;
16public:
17 stupid( char const * n ) : my_name(n) {
18 cout << "contruct stupid(\"" << my_name << "\")\n" ;
19 }
20 ~stupid() {
21 cout << "destroy stupid(\"" << my_name << "\")\n" ;
22 }
23} ;
24
25// declare prototype
26void a() ;
27void b() ;
28void c() ;
29
30long nw = 1ul<<30 ;
31
32void
33a() {
34 cout << "Enter in routine a\n" ;
35 stupid sa("sa") ;
36 auto_ptr<char> ptr(new char[nw]) ;
37 for ( long i = 0 ; i < nw ; i += 100 ) ptr.get()[i] = i % 234 ;
38 cout << "go ?" ; int a ; cin >> a ;
39 b() ;
40 cout << "Exit from routine a\n" ;
41} ;
42
43void
44b() {
45 cout << "Enter in routine b\n" ;
46 stupid sb("sb") ;
47 auto_ptr<char> ptr(new char[nw]) ;
48 for ( long i = 0 ; i < nw ; i += 100 ) ptr.get()[i] = i % 234 ;
49 cout << "go ?" ; int a ; cin >> a ;
50 c() ;
51 cout << "Exit from routine b\n" ;
52} ;
53
54void
55c() {
56 cout << "Enter in routine c\n" ;
57 stupid sc("sc") ;
58 auto_ptr<char> ptr(new char[nw]) ;
59 for ( long i = 0 ; i < nw ; i += 100 ) ptr.get()[i] = i % 234 ;
60 cout << "go ?" ; int a ; cin >> a ;
61 // make an exception
62 throw runtime_error("pippo") ;
63 cout << "Exit from routine c\n" ;
64} ;
65
66int
67main() {
68 try {
69 a() ;
70 }
71 catch ( exception const & e ) {
72 cout << "exception: " << e . what() << '\n' ;
73 }
74 catch ( ... ) {
75 cout << "found an exception!\n" ;
76 }
77 cout << "finito ?" ;
78 int a ; cin >> a ;
79 return 0 ;
80}