PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > try catch g++ syntax problem
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
try catch g++ syntax problem

Réponse
 
LinkBack Outils de la discussion
Vieux 05/06/2008, 16h15   #1
Suresh Jeevanandam
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut try catch g++ syntax problem

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
  Réponse avec citation
Vieux 05/06/2008, 16h22   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: try catch g++ syntax problem

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
  Réponse avec citation
Vieux 05/06/2008, 16h29   #3
Suresh Jeevanandam
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: try catch g++ syntax problem

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
  Réponse avec citation
Vieux 05/06/2008, 16h47   #4
Frank Birbacher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: try catch g++ syntax problem

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
  Réponse avec citation
Vieux 05/06/2008, 16h55   #5
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: try catch g++ syntax problem

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
  Réponse avec citation
Vieux 05/06/2008, 19h47   #6
Christian Hackl
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: try catch g++ syntax problem

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
  Réponse avec citation
Vieux 05/06/2008, 20h09   #7
Frank Birbacher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: try catch g++ syntax problem

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
  Réponse avec citation
Vieux 06/06/2008, 08h36   #8
Michael DOUBEZ
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: try catch g++ syntax problem

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() is
rarely informative when thrown by the STL but I agree it is not an excuse.

--
Michael
  Réponse avec citation
Vieux 06/06/2008, 11h23   #9
Christian Hackl
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: try catch g++ syntax problem

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
  Réponse avec citation
Vieux 06/06/2008, 11h58   #10
Michael DOUBEZ
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: try catch g++ syntax problem

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
  Réponse avec citation
Vieux 06/06/2008, 16h55   #11
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: try catch g++ syntax problem

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
  Réponse avec citation
Vieux 06/06/2008, 17h21   #12
Christian Hackl
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: try catch g++ syntax problem

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 18h17.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,20120 seconds with 20 queries