|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hello everybody!
I am using g++ 3.2.3. When I try to do try{} catch{} it works fine if I catch(int a). But if I do catch(SomeException e) it raises syntax errors. Any guess whats wrong? #include "tf.hh" #include <exception> #include <iostream> using namespace std; void tf_init() { libconfig::Config pltech; try { pltech.readFile("/proj/eda3/sureshj/placer/samples/techfile"); } catch(Exception e) { cout << e.what() << endl; } } tf.cc: In function `void tf_init()': tf.cc:14: syntax error before `e' make: *** [tf.o] Error 1 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Suresh Jeevanandam wrote:
> I am using g++ 3.2.3. When I try to do try{} catch{} it works fine if > I catch(int a). But if I do catch(SomeException e) it raises syntax > errors. Any guess whats wrong? > > > #include "tf.hh" > #include <exception> > #include <iostream> > > using namespace std; > > void tf_init() > { > libconfig::Config pltech; > try > { > pltech.readFile("/proj/eda3/sureshj/placer/samples/techfile"); > } > catch(Exception e) > { > cout << e.what() << endl; > } > } > > tf.cc: In function `void tf_init()': > tf.cc:14: syntax error before `e' > make: *** [tf.o] Error 1 You probably meant to use 'exception' and not 'Exception'. C++ is case sensitive. Of course, the compiler should be a bit more verbose and tell you that 'Exception' symbol is undefined, not "a syntax error"... V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Jun 5, 8:22 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> Suresh Jeevanandam wrote: > > I am using g++ 3.2.3. When I try to do try{} catch{} it works fine if > > I catch(int a). But if I do catch(SomeException e) it raises syntax > > errors. Any guess whats wrong? > > > #include "tf.hh" > > #include <exception> > > #include <iostream> > > > using namespace std; > > > void tf_init() > > { > > libconfig::Config pltech; > > try > > { > > pltech.readFile("/proj/eda3/sureshj/placer/samples/techfile"); > > } > > catch(Exception e) > > { > > cout << e.what() << endl; > > } > > } > > > tf.cc: In function `void tf_init()': > > tf.cc:14: syntax error before `e' > > make: *** [tf.o] Error 1 > > You probably meant to use 'exception' and not 'Exception'. C++ is case > sensitive. Of course, the compiler should be a bit more verbose and > tell you that 'Exception' symbol is undefined, not "a syntax error"... > > V > -- > Please remove capital 'A's when replying by e-mail > I do not respond to top-posted replies, please don't ask That solved the problem. Thanks a lot. - Suresh |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Hi!
Victor Bazarov schrieb: >> catch(Exception e) >> { [snip] > You probably meant to use 'exception' and not 'Exception'. C++ is case > sensitive. Of course, the compiler should be a bit more verbose and > tell you that 'Exception' symbol is undefined, not "a syntax error"... But for std::exception you need a reference, don't you? catch(std::exception const& e) { std::exception is abstract AFAIK. Frank |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Frank Birbacher wrote:
> Hi! > > Victor Bazarov schrieb: >>> catch(Exception e) >>> { > [snip] >> You probably meant to use 'exception' and not 'Exception'. C++ is >> case sensitive. Of course, the compiler should be a bit more verbose >> and tell you that 'Exception' symbol is undefined, not "a syntax >> error"... > > But for std::exception you need a reference, don't you? > > catch(std::exception const& e) > { > > std::exception is abstract AFAIK. If it were, the compiler would have told him that, don't you think? No, 'std::exception' isn't abstract. V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Victor Bazarov wrote:
> Frank Birbacher wrote: >> Victor Bazarov schrieb: >>>> catch(Exception e) >>>> { >> [snip] >>> You probably meant to use 'exception' and not 'Exception'. [...] >> But for std::exception you need a reference, don't you? >> >> catch(std::exception const& e) >> { >> >> std::exception is abstract AFAIK. > > If it were, the compiler would have told him that, don't you think? How so? The OP wrote "Exception" and not "exception" ![]() (Of course, the OP should catch it by reference, anyway, even if it's not an abstract class.) -- Christian Hackl |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Hi!
Victor Bazarov schrieb: > If it were, the compiler would have told him that, don't you think? No, > 'std::exception' isn't abstract. Yes, I thought the compiler would tell. Obviously I didn't try. But as Christian said: catch by value is a *bad* idea anyway. So I never needed to know std::exception wasn't abstract. Frank |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Frank Birbacher a écrit :
> Hi! > > Victor Bazarov schrieb: >> If it were, the compiler would have told him that, don't you think? >> No, 'std::exception' isn't abstract. > > Yes, I thought the compiler would tell. Obviously I didn't try. But as > Christian said: catch by value is a *bad* idea anyway. So I never needed > to know std::exception wasn't abstract. It is not so bad if you don't mind slicing . The content of what() israrely informative when thrown by the STL but I agree it is not an excuse. -- Michael |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
Michael DOUBEZ wrote:
> Frank Birbacher a écrit : >> Victor Bazarov schrieb: >>> No, 'std::exception' isn't abstract. >> Yes, I thought the compiler would tell. Obviously I didn't try. But as >> Christian said: catch by value is a *bad* idea anyway. So I never needed >> to know std::exception wasn't abstract. > > It is not so bad if you don't mind slicing . The content of what() is> rarely informative when thrown by the STL but I agree it is not an excuse. What about the extra copy required when you catch by value? That's an advantage of catching by reference even if there are no virtual functions in the class. Of course, in the OP's program it probably does not matter at all performance-wise, but it's still a standard idiom of the language that you should always follow unless you have a very good reason not to. (IMHO) -- Christian Hackl |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
Christian Hackl a écrit :
> Michael DOUBEZ wrote: > >> Frank Birbacher a écrit : >>> Victor Bazarov schrieb: >>>> No, 'std::exception' isn't abstract. >>> Yes, I thought the compiler would tell. Obviously I didn't try. But >>> as Christian said: catch by value is a *bad* idea anyway. So I never >>> needed to know std::exception wasn't abstract. >> >> It is not so bad if you don't mind slicing . The content of what()>> is rarely informative when thrown by the STL but I agree it is not >> an excuse. > > What about the extra copy required when you catch by value? That's an > advantage of catching by reference even if there are no virtual > functions in the class. Given that std::exception isn't likely to have any member, that shouldn't be a hit. However it is not the point. I agree that object exceptions should be caught by reference. -- Michael |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
On Jun 6, 12:23 pm, Christian Hackl <ha...@sbox.tugraz.at> wrote:
[...] > What about the extra copy required when you catch by value? > That's an advantage of catching by reference even if there are > no virtual functions in the class. Be serious. We're talking about exception handling here. One copy more or less isn't going to make any difference. -- James Kanze (GABI Software) email:james.kanze@gmail.com Conseils en informatique orientée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
James Kanze wrote:
> On Jun 6, 12:23 pm, Christian Hackl <ha...@sbox.tugraz.at> wrote: > > [...] >> What about the extra copy required when you catch by value? >> That's an advantage of catching by reference even if there are >> no virtual functions in the class. > > Be serious. We're talking about exception handling here. One > copy more or less isn't going to make any difference. I know. But that's basically what I said in the paragraph right after the one you quoted, didn't I? In hindsight, I probably made it look as if I put too much emphasis on that particular point. -- Christian Hackl |
|
![]() |
| Outils de la discussion | |
|
|