473,394 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 4735
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: kaeli | last post by:
Hey all, I'd like to start using the try/catch construct in some scripts. Older browsers don't support this. What's the best way to test for support for this construct so it doesn't kill...
4
by: Abhishek Srivastava | last post by:
Hello All, I have seen code snippets like try { ..... } catch {
37
by: clintonG | last post by:
Has somebody written any guidelines regarding how to determine when try-catch blocks should be used and where their use would or could be considered superfluous? <%= Clinton Gallagher...
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
13
by: Woody Splawn | last post by:
I have a try catch statement in a fucntion that is supposed to return a true or a false My code looks like this: Try mySqlConnection.Open() Dim Da1 As New SqlDataAdapter("Select JnlType,...
34
by: Bob | last post by:
Hi, The compiler gives Warning 96 Variable 'cmdSource' is used before it has been assigned a value. A null reference exception could result at runtime. Dim cmdSource as SQlClient.SQLDataReader...
12
by: reycri | last post by:
While the following is allowed: if (a == b) SomeFunction(); else OtherFunction(); The following is not: try
6
by: mast2as | last post by:
I have posted a few messages in the last few days about a project I am working on which is a quite simple parser. I ended up using the try/ catch structure as a general mechanism to control what's...
0
by: Nick Keighley | last post by:
Hi, I have, unsuccessfully, checked the net and Stroustrup before I posted here. Is there a way to catch multiple exception types in a single catch statement? Something like: try
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.