470,848 Members | 1,801 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

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
Jun 27 '08 #1
9 4621
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
Jun 27 '08 #2
On Jun 5, 8:22 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
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
Jun 27 '08 #3
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
Jun 27 '08 #4
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
Jun 27 '08 #5
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
Jun 27 '08 #6
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
Jun 27 '08 #7
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
Jun 27 '08 #8
On Jun 6, 12:23 pm, Christian Hackl <ha...@sbox.tugraz.atwrote:

[...]
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:ja*********@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
Jun 27 '08 #9
James Kanze wrote:
On Jun 6, 12:23 pm, Christian Hackl <ha...@sbox.tugraz.atwrote:

[...]
>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
Jun 27 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by kaeli | last post: by
4 posts views Thread by Abhishek Srivastava | last post: by
37 posts views Thread by clintonG | last post: by
23 posts views Thread by VB Programmer | last post: by
13 posts views Thread by Woody Splawn | last post: by
reply views Thread by Nick Keighley | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.