[VISUAL C++ 6]Probleme de try .. catch

Probleme de try .. catch [VISUAL C++ 6] - C++ - Programmation

Marsh Posté le 30-12-2002 à 14:25:32    

bon bref, je mets au point une classe template de gestion des intervals :
 

Code :
  1. /*!
  2.      *********************************************************************
  3.      * @brief Range error exception class.
  4.      *
  5.      * The Error class is solely created to be thrown by the
  6.      * Range class when a range error is caught.
  7.      *
  8.      * <code>
  9.      * try
  10.      * {
  11.      *   Range<int,0,5> x = 3;  // OK
  12.      *   Range<int,0,5> y = 8;  // KO
  13.      * }
  14.      * catch( Range::Error )
  15.      * {
  16.      *    cout << " A Range error has been caught !!" << endl;
  17.      * }
  18.      * </code>
  19.      *
  20.      *
  21.      *********************************************************************
  22.     */
  23.     class RangeError {};
  24.     /*!
  25.      *********************************************************************
  26.      * @brief Value range template.
  27.      *
  28.      * The Range template allow users to create a type representing a
  29.      * contiguous range of value. It could be transparently used to specify
  30.      * function prototypes :
  31.      *
  32.      * <code>
  33.      * double arcSinus( Range<double,-1.0,1.0> val );
  34.      * </code>
  35.      *  
  36.      * The template parameters describes the types of the range data,
  37.      * the lower bound and the higher bound of the Range.
  38.      *
  39.      * If val is out of the [-1,1[ bound, a Error exception is  
  40.      * thrown and could be handled. Note that the range is open on the  
  41.      * upper end. When calling arcSinus, you use the normal way to call it :
  42.      *
  43.      * <code>
  44.      * double r = arcSinus(0.25); // valid
  45.      *
  46.      * r = arcSinus( 2.5);        // throws Error.
  47.      * </code>
  48.      *
  49.      * Note also that we must initialize any Range object when we create
  50.      * it hence there is no default constructor for it.
  51.      *
  52.      * <code>
  53.      * Range<char,'a','z'> letter;  // KO
  54.      *
  55.      * Range<char,'a','z'> letter = 'e';  // OK
  56.      * </code>
  57.      *
  58.      *********************************************************************
  59.     */
  60.     template< typename T, T low, T high > class Range 
  61.     {
  62.         public:
  63.         Range( T val ) { Assert<RangeError>((val>=low) && (val<high)); value = val; }
  64.         Range operator=( T val ) { return *this=Range(val); }
  65.         operator T() { return value; }
  66.         private:
  67.         T value;
  68.     };


 
La fonction Assert<> est :
 

Code :
  1. template<class E, class A> Assert( A assertion )
  2.     {
  3.         if( !assertion ) throw E();   
  4.     }


 
Tout a l'air OK, suaf que quand je compile la chose :
 

Code :
  1. try
  2.     {
  3.         Range<int,0,15> x = 2;
  4.         cout << "x : " << x << endl;
  5.         Range<int,0,5> y = 8;
  6.         cout << "y : " << y << endl; // Jamais executé ...
  7.     }
  8.     catch( RangeError )
  9.     {
  10.         cout << "Erreur de range !!" << endl;
  11.     }


 
x passe et au lieu de prendre la branche catch a la declarration de y, c'est une Unhandled Exception de KERNEL32 qui est levée ...
Moi pas comprendre ...


Message édité par Joel F le 30-12-2002 à 14:27:51
Reply

Marsh Posté le 30-12-2002 à 14:25:32   

Reply

Marsh Posté le 30-12-2002 à 18:13:02    

J'ai essaye ton code avec g++ 3.2.1-2. Ca marche très bien, sauf que tu devrais dire que le type de retour de la function "Assert" est "void".
 
A mon humble avis, Visual C++ 6 a peut-etre des problèmes avec les exceptions de classe template...
 
Regarde en debug en pas à pas. Désolé la j'ai pas Visual C++ 6 sur moi (seulement au boulot)

Reply

Marsh Posté le 30-12-2002 à 21:33:05    

oui le 'void' ct une faute de frappe.
bon apparemment c le gestionnaire d'exception de VC6 qui chie dans la colle ...
euh le Range marche bien alors, j'ai eu un doute a un moment ...

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed