Syntax Error in throw().

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Euripides J. Sellountos

    Syntax Error in throw().

    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@O REMUS
    $ 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.j.s
  • Euripides J. Sellountos

    #2
    Re: Syntax Error in throw().

    Euripides J. Sellountos wrote:
    [color=blue]
    > 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@O REMUS
    > $ cat error.cpp
    >
    > #include <iostream>
    > class error {};
    >
    > class A {
    > public:
    > A() throw(error);
    > };
    >
    > A::A() throw(error) {
    > throw(error()); <--- HERE is the syntax error[/color]
    ^^^^
    sorry for the uppercase.
    No, I am not screaming.[color=blue]
    > }[/color]

    If I do the following it works.
    But I don't understand....
    A::A() throw(error) {
    error a;
    throw(a);
    }
    [color=blue]
    >
    > int main () {
    > try {
    > A x;
    > } catch(error a) {
    > std::cout<<"Ok\ n";
    > }
    > return 0;
    > }
    >
    > Any help is appreciated.
    >[/color]


    --
    e.j.s

    Comment

    • Jeff Flinn

      #3
      Re: Syntax Error in throw().


      "Euripides J. Sellountos" <sellountos@mec h.upatras.gr> wrote in message
      news:c4jsen$ed8 $1@nic.grnet.gr ...[color=blue]
      > Euripides J. Sellountos wrote:
      >[color=green]
      > > 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@O REMUS
      > > $ cat error.cpp
      > >
      > > #include <iostream>
      > > class error {};
      > >
      > > class A {
      > > public:
      > > A() throw(error);
      > > };
      > >
      > > A::A() throw(error) {
      > > throw(error()); <--- HERE is the syntax error[/color][/color]

      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



      Comment

      • Jeff Schwab

        #4
        Re: Syntax Error in throw().

        Euripides J. Sellountos wrote:
        [color=blue]
        > The following snippet fails to compile with g++.[/color]

        <snip />
        [color=blue]
        > throw(error()); <--- HERE is the syntax error[/color]



        Comment

        • Ahti Legonkov

          #5
          Re: Syntax Error in throw().

          Euripides J. Sellountos wrote:
          [color=blue]
          > 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
          >[/color]
          [code..][color=blue]
          >
          > A::A() throw(error) {
          > throw(error()); <--- HERE is the syntax error[/color]

          //You have extra parentheses here. Remove them and it should be ok:
          throw error();
          [color=blue]
          > }[/color]
          [code..]

          --
          Ahti Legonkov

          Comment

          • Ron Natalie

            #6
            Re: Syntax Error in throw().


            "Euripides J. Sellountos" <sellountos@mec h.upatras.gr> wrote in message news:c4js59$e86 $1@nic.grnet.gr ...
            [color=blue]
            > error.cpp: In constructor `A::A()':
            > error.cpp:11: parse error before `;' token
            >
            >[/color]
            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.

            Comment

            • Euripides J. Sellountos

              #7
              Re: Syntax Error in throw().

              Ron Natalie wrote:[color=blue]
              > "Euripides J. Sellountos" <sellountos@mec h.upatras.gr> wrote in message news:c4js59$e86 $1@nic.grnet.gr ...
              >
              >[color=green]
              >>error.cpp: In constructor `A::A()':
              >>error.cpp:1 1: parse error before `;' token
              >>
              >>[/color]
              >
              > 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.
              >[/color]

              Thanks everyone for the helpful responses.
              It works now.

              e.j.s.

              --
              e.j.s

              Comment

              Working...