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

  • Home
  • SEARCH
  • 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 244685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:01:31+00:00 2026-05-11T21:01:31+00:00

What are the performance implications of throwing exceptions in C++0x? How much is this

  • 0

What are the performance implications of throwing exceptions in C++0x? How much is this compiler dependent? This is not the same as asking what is the cost of entering a try block, even if no exception is thrown.

Should we expect to use exceptions more for general logic handling like in Java?

  • 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-05-11T21:01:32+00:00Added an answer on May 11, 2026 at 9:01 pm
    #include <iostream>
    #include <stdexcept>
    
    struct SpaceWaster {
        SpaceWaster(int l, SpaceWaster *p) : level(l), prev(p) {}
        // we want the destructor to do something
        ~SpaceWaster() { prev = 0; }
        bool checkLevel() { return level == 0; }
        int level;
        SpaceWaster *prev;
    };
    
    void thrower(SpaceWaster *current) {
        if (current->checkLevel()) throw std::logic_error("some error message goes here\n");
        SpaceWaster next(current->level - 1, current);
        // typical exception-using code doesn't need error return values
        thrower(&next);
        return;
    }
    
    int returner(SpaceWaster *current) {
        if (current->checkLevel()) return -1;
        SpaceWaster next(current->level - 1, current);
        // typical exception-free code requires that return values be handled
        if (returner(&next) == -1) return -1;
        return 0;
    }
    
    int main() {
        const int repeats = 1001;
        int returns = 0;
        SpaceWaster first(1000, 0);
    
        for (int i = 0; i < repeats; ++i) {
            #ifdef THROW
                try {
                    thrower(&first);
                } catch (std::exception &e) {
                    ++returns;
                }
            #else
                returner(&first);
                ++returns;
            #endif
        }
        #ifdef THROW
            std::cout << returns << " exceptions\n";
        #else
            std::cout << returns << " returns\n";
        #endif
    }
    

    Mickey Mouse benchmarking results:

    $ make throw -B && time ./throw
    g++     throw.cpp   -o throw
    1001 returns
    
    real    0m0.547s
    user    0m0.421s
    sys     0m0.046s
    
    $ make throw CPPFLAGS=-DTHROW -B && time ./throw
    g++  -DTHROW   throw.cpp   -o throw
    1001 exceptions
    
    real    0m2.047s
    user    0m1.905s
    sys     0m0.030s
    

    So in this case, throwing an exception up 1000 stack levels, rather than returning normally, takes about 1.5ms. That includes entering the try block, which I believe on some systems is free at execution time, on others incurs a cost each time you enter try, and on others only incurs a cost each time you enter the function which contains the try. For a more likely 100 stack levels, I upped the repeats to 10k because everything was 10 times faster. So the exception cost 0.1ms.

    For 10 000 stack levels, it was 18.7s vs 4.1s, so about 14ms additional cost for the exception. So for this example we’re looking at a pretty consistent overhead of 1.5us per level of stack (where each level is destructing one object).

    Obviously C++0x doesn’t specify performance for exceptions (or anything else, other than big-O complexity for algorithms and data structures). I don’t think it changes exceptions in a way which will seriously impact many implementations, either positively or negatively.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 289k
  • Answers 289k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer "em" is available as a relative unit of measure in… May 13, 2026 at 5:29 pm
  • Editorial Team
    Editorial Team added an answer I'm not answering your question as asked, but why are… May 13, 2026 at 5:29 pm
  • Editorial Team
    Editorial Team added an answer You can define error handling pages in your web.xml: <error-page>… May 13, 2026 at 5:29 pm

Related Questions

Question is as stated in the title: What are the performance implications of marking
I'm designing a database schema, and I'm wondering what criteria I should use for
I remember from C days that we were encouraged to use i > -1
I've started to use mysql_data_seek () as an easy way to roll pagination transparently

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.