| 1 | #include <stdlib.h>
 | 
|---|
| 2 | #include <stdio.h>
 | 
|---|
| 3 | 
 | 
|---|
| 4 | #include "lance.h"
 | 
|---|
| 5 | 
 | 
|---|
| 6 | 
 | 
|---|
| 7 | 
 | 
|---|
| 8 | Toto::Toto(int n)
 | 
|---|
| 9 | {
 | 
|---|
| 10 |   if (n < 1)  throw(myException(ExcLongMessage("Toto::Toto() Size<1")) );
 | 
|---|
| 11 |   size = n;
 | 
|---|
| 12 |   data = new double[n];
 | 
|---|
| 13 |   for(int k=0; k<n; k++) data[k] = k;
 | 
|---|
| 14 | }
 | 
|---|
| 15 | 
 | 
|---|
| 16 | Toto::~Toto()
 | 
|---|
| 17 | {
 | 
|---|
| 18 |   delete[] data;
 | 
|---|
| 19 | }
 | 
|---|
| 20 | 
 | 
|---|
| 21 | 
 | 
|---|
| 22 | int lance(int a, long sz);
 | 
|---|
| 23 | int main(int narg, char *arg[])
 | 
|---|
| 24 | {
 | 
|---|
| 25 |   if (narg < 3) {
 | 
|---|
| 26 |     cout << " Usage: lance 0/1/2/3/5 size \n" 
 | 
|---|
| 27 |          << "  0: Rien, 1: throw myException, 2: throw int \n" 
 | 
|---|
| 28 |          << "  3: throw exception 4: new double[size] " 
 | 
|---|
| 29 |          << "  5: class toto+acces aux elts"  << endl;
 | 
|---|
| 30 |     return(0);
 | 
|---|
| 31 |   }
 | 
|---|
| 32 | 
 | 
|---|
| 33 |   int rc,a;
 | 
|---|
| 34 |   long sz;
 | 
|---|
| 35 |   a = atoi(arg[1]);
 | 
|---|
| 36 |   sz = atoi(arg[2]);
 | 
|---|
| 37 |   cout << "\n ---- Lance , test exeception, a=" << a << " Sz=" << sz << endl; 
 | 
|---|
| 38 |   try {
 | 
|---|
| 39 |     rc = lance(a, sz);
 | 
|---|
| 40 |     cout << " Return Code from lance= " << rc << endl;
 | 
|---|
| 41 |   }
 | 
|---|
| 42 |   catch(myException exc){
 | 
|---|
| 43 |     cerr << " Catched myException : msg= " << exc.Msg() << endl;
 | 
|---|
| 44 |     rc = 77;
 | 
|---|
| 45 |   }
 | 
|---|
| 46 |   catch (int ie) {
 | 
|---|
| 47 |     cerr << "  Catched exception - integer= " << ie << endl;
 | 
|---|
| 48 |     rc = 78;
 | 
|---|
| 49 |   }
 | 
|---|
| 50 |   catch(exception exc){
 | 
|---|
| 51 |   // Classe de base des exception standard - 
 | 
|---|
| 52 |   // La methode what() n'est pas forcement conforme a la norme 
 | 
|---|
| 53 |     string msg = exc.what();
 | 
|---|
| 54 |     cerr << " Catched exception : msg= " << msg << endl;
 | 
|---|
| 55 |     rc = 98;
 | 
|---|
| 56 |   }
 | 
|---|
| 57 |   catch (...) {
 | 
|---|
| 58 |     cerr << " Catched Unknown exception ! " << endl;
 | 
|---|
| 59 |     rc = 99;
 | 
|---|
| 60 |   }
 | 
|---|
| 61 | 
 | 
|---|
| 62 |   cout << " ------ Exiting from lance ----------------- \n" << endl;
 | 
|---|
| 63 |   exit(rc);
 | 
|---|
| 64 | }
 | 
|---|
| 65 | 
 | 
|---|
| 66 | 
 | 
|---|
| 67 | int lance(int a, long sz)
 | 
|---|
| 68 | {
 | 
|---|
| 69 |   int rc = 0;
 | 
|---|
| 70 |   char buff[128];
 | 
|---|
| 71 |   double *d;
 | 
|---|
| 72 |   switch(a){
 | 
|---|
| 73 |   case 0:
 | 
|---|
| 74 |     cout << " lance-0 / OK " << endl;
 | 
|---|
| 75 |     break;
 | 
|---|
| 76 |   case 1:
 | 
|---|
| 77 |     cout << " lance-1 / throw myException " << endl;
 | 
|---|
| 78 |     sprintf(buff,"lance() - File=%s Line=%d",__FILE__,__LINE__);
 | 
|---|
| 79 |     throw myException(buff);
 | 
|---|
| 80 |     break;
 | 
|---|
| 81 |   case 2:
 | 
|---|
| 82 |     cout << " lance-2 / throw integer 2001 " << endl;
 | 
|---|
| 83 |     rc = 2001;
 | 
|---|
| 84 |     throw rc;
 | 
|---|
| 85 |     break;
 | 
|---|
| 86 |   case 3:
 | 
|---|
| 87 |     cout << " lance-3 / throw execption " << endl;
 | 
|---|
| 88 |     throw exception();
 | 
|---|
| 89 |     break;
 | 
|---|
| 90 |   case 4:
 | 
|---|
| 91 |     cout << " lance-4 / new double[ " << sz << " ]" << endl;
 | 
|---|
| 92 |     d = new double[sz];
 | 
|---|
| 93 |     rc = 4;
 | 
|---|
| 94 |     break;
 | 
|---|
| 95 |   case 5:
 | 
|---|
| 96 |     {
 | 
|---|
| 97 |     cout << " lance-5 / Toto( " << sz << " )" << endl;
 | 
|---|
| 98 |     Toto t(sz);
 | 
|---|
| 99 |     double s=0.;
 | 
|---|
| 100 |     for(int kk=0; kk<=sz; kk++)   s+= t.Value(kk);
 | 
|---|
| 101 |     cout << " Somme= " << s << endl;
 | 
|---|
| 102 |     }
 | 
|---|
| 103 |   default:
 | 
|---|
| 104 |     rc = 9;
 | 
|---|
| 105 |     break;
 | 
|---|
| 106 |   }
 | 
|---|
| 107 |   return(rc);
 | 
|---|
| 108 | }
 | 
|---|