Bonjour

J'ai une question de base sur les gestions d'erreurs. Je ne comprends pas pourquoi le code suivant fonctionne:


Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
int f(int i) {
   throw "test";
   return 0;
}
 
class C {
   int i;
 
public:
   C(int);
};
 
C::C(int ii) {
   try {   // function-try block
      f(ii) ;
      // body of function goes in try block
   }
   catch (...) {
      // handles exceptions thrown from the constructor-initializer
      // and from the constructor function body
      printf_s("In the catch block\n");
   }
}
 
int main() {
   C *MyC = new C(0);
   delete MyC;
}

alors que celui là, où j'ai juste remplacé catch(...) par catch(char*),

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
int f(int i) {
   throw "test";
   return 0;
}
 
class C {
   int i;
 
public:
   C(int);
};
 
C::C(int ii) {
   try {   // function-try block
      f(ii) ;
      // body of function goes in try block
   }
   catch (char*) {
      // handles exceptions thrown from the constructor-initializer
      // and from the constructor function body
      printf_s("In the catch block\n");
   }
}
 
int main() {
   C *MyC = new C(0);
   delete MyC;
}
ne fonctionne pas.

Je pensais que throw renvoyait une exception de type char*, mais, bien que cette exception se situait à un niveau plus inférieur que catch(char*), celle-ci remonterait pour être attrapée par ce catch. Mais ce n'est pas le cas apparemment.

Pourriez vous m'expliquer pourquoi?

Il semble que catch(...) soit un catch par défaut.


Merci