Syntax Error in throw().

  • Thread starter Euripides J. Sellountos
  • Start date
E

Euripides J. Sellountos

Hello kind people.
I hope you can you help me with the following problem.

The following snippet fails to compile with g++.
(It compiles fine with other compilers.)
All I want to do is to throw an "error" exception
when the constructor A::A() gets called. I don't
understand. Do i really have any syntax error?

I'm receiving the following error message

error.cpp: In constructor `A::A()':
error.cpp:11: parse error before `;' token


Administrator@OREMUS
$ cat error.cpp

#include <iostream>
class error {};

class A {
public:
A() throw(error);
};

A::A() throw(error) {
throw(error()); <--- HERE is the syntax error
}

int main () {
try {
A x;
} catch(error a) {
std::cout<<"Ok\n";
}
return 0;
}

Any help is appreciated.
 
E

Euripides J. Sellountos

Euripides said:
Hello kind people.
I hope you can you help me with the following problem.

The following snippet fails to compile with g++.
(It compiles fine with other compilers.)
All I want to do is to throw an "error" exception
when the constructor A::A() gets called. I don't
understand. Do i really have any syntax error?

I'm receiving the following error message

error.cpp: In constructor `A::A()':
error.cpp:11: parse error before `;' token


Administrator@OREMUS
$ cat error.cpp

#include <iostream>
class error {};

class A {
public:
A() throw(error);
};

A::A() throw(error) {
throw(error()); <--- HERE is the syntax error
^^^^
sorry for the uppercase.
No, I am not screaming.

If I do the following it works.
But I don't understand....
A::A() throw(error) {
error a;
throw(a);
}
 
J

Jeff Flinn

Just a guess at a workaround, how about:

throw error();

Since 'throw' is a statement and not a function. I'm not a g++ user so I
can't test this out. As you say, the original compiles/links/runs on VC7.1.

I assume that A is a simplification of some more complex situation.
Otherwise you'd be better off declaring A() private and making a call to the
constructor a compilation rather than a run-time error.

Jeff F
 
A

Ahti Legonkov

Euripides said:
The following snippet fails to compile with g++.
(It compiles fine with other compilers.)
All I want to do is to throw an "error" exception
when the constructor A::A() gets called. I don't
understand. Do i really have any syntax error?

I'm receiving the following error message
[code..]

A::A() throw(error) {
throw(error()); <--- HERE is the syntax error

//You have extra parentheses here. Remove them and it should be ok:
throw error();
[code..]
 
R

Ron Natalie

Euripides J. Sellountos said:
error.cpp: In constructor `A::A()':
error.cpp:11: parse error before `;' token
Program looks OK to me (by the way, throw() is not a function, the parens
on this line are unnecessary).

Just for jollies, what happens if you rename the error class something like
MyError? Might be (this isn't supposed to happen) that some thing that
<iostream> includes on your implementation may define error.
 
E

Euripides J. Sellountos

Ron said:
Program looks OK to me (by the way, throw() is not a function, the parens
on this line are unnecessary).

Just for jollies, what happens if you rename the error class something like
MyError? Might be (this isn't supposed to happen) that some thing that
<iostream> includes on your implementation may define error.

Thanks everyone for the helpful responses.
It works now.

e.j.s.
 

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

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top