Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8227261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:04:04+00:00 2026-06-07T16:04:04+00:00

Edit: there’s nothing wrong with the book, I simply failed to properly copy the

  • 0

Edit: there’s nothing wrong with the book, I simply failed to properly copy the code provided in it. The try-catch block is supposed to be inside the while loop while I’ve put it outside of it.


There is something I may be missing about Stroutrup’s “Programming: Principles and Practice using C++”, at chapter 7, section 7 (“Recovering from errors”). I hope someone who read this book will be able to help me (or anyone, actually!).

We have developed a calculator with a token parser. When the program reads an invalid token, it just terminates. Section 7′ objective is to make the calculator recover from such errors without terminating. My problem is that after following the book’s directives, the calculator still terminates after an invalid token.

Here is the complete code of the calculator. Here is std_lib_facilities.h, in case you need it to understand the code.

As you can see, main() calls for calculate(), which in turn calls for clean_up_mess() if an exception is thrown (which happens when a invalid token is read). clean_up_mess() then removes everything from the Token_stream until the print char (‘;’) is found, so we can go on with the next calculation, which will hopefully not contain another invalid token.

But after the exception handling is done, the program simply terminates. What do I need to do for it to resume where the exception was thrown? And did the author forget to explain this, or did I miss something?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:04:05+00:00Added an answer on June 7, 2026 at 4:04 pm

    After handling an exception, C++ continues execution with the code after the catch block. It does not “resume” from where the exception was thrown.

    The solution would be to move the try/catch block inside the while loop of calculate():

    void calculate()
    {
        while (cin) {
            try {
                cout << prompt;
                Token t = ts.get();
                while (t.kind == print)
                        t=ts.get();
                if (t.kind == quit) {
                        return;
                }
                ts.putback(t);
                cout << result << expression() << endl;
             } catch (exception& e) {
                 cerr << e.what() << endl;
                 clean_up_mess();
             }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any possible way to edit this code <asp:LoginName ID=LoginName1 runat=server style=top: 165px;
EDIT: There was something wrong with my Base64 decoding. I searched for a external
I'm using the code found in this jsbin: http://jsbin.com/asahe5/10/edit There is a function within
thirtydot edit: (there's no doctype) Here is the code in pastebin: http://pastebin.com/zwPTd8dd <html> <head>
Why set it to null first ? [EDIT] there was this code about reading
EDIT: There must be some way I can approach this without writing a whole
EDIT: There's now a doc page on this so this question is irrelevant, also
**EDIT: There are several options below that would work. Please vote/comment according to your
EDIT: There is the almost same question at Removing whitespace between HTML elements when
EDIT - there are no blank lines. EDIT 2 - my bad, it seems

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.