Thread: Parse error in catch statement

  1. #1
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300

    Parse error in catch statement

    I'm trying to do an exercise in Accelerated C++, and I'm taking this part of the code straight from the book. However, it will not compile and I can't figure out why.
    Code:
    double grade_aux(const Student_info& s)
    {
        try
        {
            return grade(s);
        }
        catch (domain_error)
        {
            return grade(s.midterm, s.final, 0);
        }
    }
    Unless I'm blind, the code is typed in just the same as in the book, and is consistent with what I know of catching exceptions.
    I'm using Dev-C++ on Win XP. If I comment out this function, the file compiles fine. It gives me:

    " Parse error before ')' " on the bolded line when I try to compile.

    Any thoughts? (Relevant to this error, anyway?)
    There is a difference between tedious and difficult.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Is there supposed to be a throw in there somewhere..??

    might need to see more code.. might be a typo....

    is grade_aux() derived from any particular class...?



    don't have the book (yet) so this is all I can offer ye' right now
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There doesn't need to be a throw, but domain_error needs to be defined, and the header that declares it must be included.

    Post the declaration of domain_error, the prototype for grade(), and the full text of the error(s).

  4. #4
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    I'm pretty sure grade is prototyped as:
    Code:
    double grade(const double&, const double&, const double&);
    and
    Code:
    double grade(const Student_info&);
    I don't have the book here at work and internet's down at home so I can't say for sure until tomorrow.
    The error is pretty much all there, just the line number is missing. There was also a "confused by previous errors, bailing out" error on the same line.

    I'm sure, however, that I forgot to #include <exception>. oops.

    Thanks for the help. If that's not it, I'll post more information tomorrow.
    There is a difference between tedious and difficult.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Oh, I didn't recognize that domain_error was part of the standard C++ exception hierarchy. Then yes, it should work if you #include <stdexcept> and specify the std namespace.

  6. #6
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    and specify the std namespace.
    I didn't realize I had to. I'm trying to only use what the book has taught so far, and so with these problems I'm still using "using std::whatever".

    If I have to use the std namespace, should I also add
    Code:
    using std::domain_error
    or something different? (I'd check the book before asking, but of course this is the day I didn't bring it with me...)
    There is a difference between tedious and difficult.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yes, using std::domain_error; will work.

    Since domain_error is part of the std namespace, you have to call it std::domain_error in your code, or put the using declaration like you mentioned, or have a global using directive. If you've been using using declarations like using std::cout, then you should be consistent and do the same for domain_error.

  8. #8
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Awesome, thanks.
    There is a difference between tedious and difficult.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    catch (domain_error)
    In addition, you are only specifying the type of the exception and not a variable. It compiles without specifying the variable, but I think you probably want to list the variable as a matter of course, just like you do when listing function parameters. Something like this:
    Code:
    catch(std::domain_error& rEx)
    What do you think of Accelerated C++? Is that your first C++ book?

  10. #10
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    I am really happy with Accelerated C++. It's well written, has a good pace and the exercises aren't just busy work; they really help me to understand the topics.

    I took a semester of C++ in college, but it was all procedural using non-standard code. (Looking back it was pretty much writing QBASIC programs with C++ syntax.) I suppose that counts as my first C++ book, but I promptly threw it away once I joined this forum and found out the textbook was absolute crap. I bought this to unlearn the bad habits I picked up in that book (void main(), etc.). Between Accelerated C++ and this forum I've learned far more than I did in that class.
    There is a difference between tedious and difficult.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM