try catch g++ syntax problem

  • Thread starter Suresh Jeevanandam
  • Start date
S

Suresh Jeevanandam

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
 
S

Suresh Jeevanandam

Suresh said:
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

That solved the problem. Thanks a lot.

-
Suresh
 
F

Frank Birbacher

Hi!

Victor said:
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
 
C

Christian Hackl

Victor said:
Frank said:
Victor said:
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.)
 
F

Frank Birbacher

Hi!

Victor said:
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
 
M

Michael DOUBEZ

Frank Birbacher a écrit :
Hi!



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.
 
C

Christian Hackl

Michael said:
Frank Birbacher a écrit :

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)
 
M

Michael DOUBEZ

Christian Hackl a écrit :
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.
 
J

James Kanze

[...]
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.
 
C

Christian Hackl

James said:
[...]
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,063
Latest member
StormyShuf

Latest Threads

Top