Lesson N.10 of July 30, 2014¶
Using exception
File example16.cc
:open:
1/*
2 example of use exception
3
4*/
5
6#include <iostream>
7#include <iomanip>
8#include <vector>
9#include <cmath>
10
11#include <string> // C++ string
12
13#include <exception> // std::exception
14
15using namespace std ;
16
17void
18throw_an_exception_level_1() {
19 cout << "Before the exception\n" ;
20 //throw "SOMETHING GO WRONG" ;
21 //throw 1234 ;
22 throw runtime_error("runtime_error used") ;
23 cout << "After the exception\n" ;
24}
25
26void
27throw_an_exception() {
28 cout << "Before throw_an_exception_level_1\n" ;
29 throw_an_exception_level_1() ;
30 cout << "After throw_an_exception_level_1\n" ;
31}
32
33int
34main() {
35
36 try {
37 // ....
38 cout << "Before the call of throw_an_exception()\n" ;
39 throw_an_exception() ;
40 cout << "After the call of throw_an_exception()\n" ;
41 // ....
42 }
43 catch ( char const error[] ) {
44 cerr << "Exception found: " << error << "\n" ;
45 }
46 catch ( int error ) {
47 cerr << "Exception number " << error << "\n" ;
48 }
49 catch ( exception const & error ) {
50 cerr << "Exception number " << error.what() << "\n" ;
51 }
52 catch ( ... ) {
53 cerr << "Exception found: Unknown error\n" ;
54 }
55 cout << "END OF PROGRAM\n" ;
56
57 return 0 ;
58}
Using class as function
File newton.cc
:open:
1/*
2 example of use exception
3*/
4
5#include "newton.hh"
6#include "myfunction.hh"
7#include "ncfunction.hh"
8#include <iostream>
9
10using namespace std ;
11
12int
13main() {
14
15 try {
16 //MyFunction<double> fun(2) ;
17 NC_Function<double> fun ;
18
19 double x = newton( fun, 400.0, 100, 1e-12 ) ;
20 cout << "x = " << x << '\n' ;
21 }
22 catch ( exception const & error ) {
23 cerr << "Exception: " << error.what() << "\n" ;
24 }
25 catch ( ... ) {
26 cerr << "Exception found: Unknown error\n" ;
27 }
28 cout << "END OF PROGRAM\n" ;
29
30 return 0 ;
31}
File newton.hh
:open:
1#ifndef NEWTON_HH
2#define NEWTON_HH
3
4#include "funclass.hh"
5#include <cmath>
6#include <stdexcept> // std::exception
7
8template <typename FUNC, typename T, typename INT >
9T // return the value of the approximated root
10newton( FUNC const & f, // instance of object function class
11 T const & x0, // initial guess
12 INT maxIter, // maximum number of iteration
13 T const & tolerance // tolereance used to stop iterations
14 ) {
15 T x = x0 ;
16 for ( INT i = 0 ; i < maxIter ; ++i ) {
17 T fun = f(x) ;
18 T Dfun = f.D(x) ;
19 if ( std::abs(Dfun) < 1E-10*std::abs(fun) )
20 throw std::runtime_error("newton, divide by (near) 0") ;
21 T h = fun/Dfun ;
22 x -= h ;
23 if ( std::abs(h) < tolerance ) return x ; // all ok!
24
25 if ( std::abs(x-x0) > 1E10 )
26 throw std::runtime_error("newton is diverging") ;
27 }
28 throw std::runtime_error("newton not converged within iteration limits") ;
29} ;
30
31#endif
File funclass.hh
:open:
1#ifndef FUNCLASS_HH
2#define FUNCLASS_HH
3
4template <typename T>
5class FunClass {
6public:
7 typedef T valueType ; // valueType is an alias for T
8private:
9 FunClass( FunClass const & ) ; // block the copy constructor
10 FunClass const & operator = ( FunClass const & ) ; // block the copy operator
11public:
12 FunClass() {}
13 virtual T operator () ( T const & val ) const = 0 ;
14 virtual T D( T const & val ) const = 0 ; // make class pure virtual
15} ;
16
17#endif
File ncfunction.hh
:open:
1#ifndef NC_FUNCTION_HH
2#define NC_FUNCTION_HH
3
4#include "funclass.hh"
5
6template <typename T>
7class NC_Function : FunClass<T> {
8public:
9 NC_Function() : FunClass<T>() {}
10 NC_Function( T const & a ) : FunClass<T>() {}
11
12 virtual T operator () ( T const & x ) const {
13 return atan(x) ;
14 }
15 virtual T D( T const & x ) const {
16 return 1/(1+x*x);
17 }
18} ;
19
20#endif
File myfunction.hh
:open:
1#ifndef MYFUNCTION_HH
2#define MYFUNCTION_HH
3
4#include "funclass.hh"
5
6template <typename T>
7class MyFunction : FunClass<T> {
8 T avalue ;
9public:
10 MyFunction() : FunClass<T>(), avalue(2) {}
11 MyFunction( T const & a ) : FunClass<T>(), avalue(a) {}
12
13 virtual T operator () ( T const & x ) const {
14 return x*x-avalue ;
15 }
16 virtual T D( T const & x ) const {
17 return 2*x ;
18 }
19} ;
20
21#endif
All the files in one zip file