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 8362973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:04:05+00:00 2026-06-09T12:04:05+00:00

I have 2 questions about non-throwing functions: Why make a function non-throwing? How to

  • 0

I have 2 questions about non-throwing functions:

  1. Why make a function non-throwing?

  2. How to make a function non-throwing? If the code inside the function actually may throw, then should I still make it non-throwing?

Here is an example:

void swap(Type t1, Type t2) throw()
{
    //swap
}

If the code in swap won’t throw at all, should I still append throw()? Why?

  • 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-09T12:04:06+00:00Added an answer on June 9, 2026 at 12:04 pm

    throw() (or noexcept in C++11) is useful for two reasons:

    1. It allows the compiler to be more aggressive in its optimisations.
    2. It tells function users that they can use this function in their own non-throwing functions.

    Non-throwing functions are very important for writing exception safe code. For instance, the usual way of writing an exception safe operator= is via a non-throwing swap() function.

    On the other hand, other exception specifications are useless and have been justly deprecated in the current standard. They don’t mix well at all with templates and are too expensive to enforce.

    Now, if you use a noexcept specification in a function that might actually throw, all bets are off. Even if your compiler does not terminate the program when an exception leaves the function (VS does not do it for runtime efficiency reasons, for instance), your code might not do what you thought because of optimisations. For example:

    void f() noexcept
    {
      a();
      b();
    }
    

    If a() actually throws and b() has side effects, the function behaviour will be unpredictable, because your compiler may decide to execute b() before a(), since you have told it that no exceptions will be thrown.

    EDIT: Now for the second part of your question: how to make a function non-throwing?

    First you need to ask yourself whether your function should really be non-throwing. For instance:

    class C
    {
      C* CreateInstance()
      {
        return new C();
      }
    }
    

    Since operator new can throw a std::bad_alloc, CreateInstance() can throw. You could try to avoid it with a try-catch block, handling or swallowing any exceptions that may be thrown inside the try block, but would that really be sensible? For instance:

    C* CreateInstance()
    {
      try
      {
         return new C();
      }
      catch (...)
      {
         return null;
      }
    }
    

    Problem solved, it seems, but would your callers be prepared for CreateInstance() returning null? If not, a crash will ensue when they try to use that pointer. Besides, a std::bad_alloc usually means that you have run out of memory and you would just be postponing the problem.

    So be careful: some functions can be made non-throwing, but others should be allowed to throw. Exception safety is not a trivial matter.

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

Sidebar

Related Questions

I have two questions about overloading. 1- Why sometimes do make overloading operators non-member
I have some questions about the default values in a function parameter list Is
I have some questions about the object size with virtual. 1) virtual function class
I am implementing my first actual non-tutorial Backbone app, and have 2-ish questions about
I have a question about removing non-alphanumeric characters from a list in R. I
I have questions about System.Threading.ThreadStart Class : where can I find its specifications (
I have few questions about ssis transction isolation levels. consider a scenario:I have an
I have some questions about importing data from Excel/CSV File into SQL Server. Let
i have some questions about constructors in ColdFusion : must i use the name
I have some questions about using MySQLi queries, and related memory management. Suppose I

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.