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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:00:13+00:00 2026-05-18T05:00:13+00:00

I can’t seem to get my head around why people say C++ exceptions are

  • 0

I can’t seem to get my head around why people say C++ exceptions are better. For example, I have an application which loads function objects from shared objects to be used in the application. What goes on is something like this:

bool LoadFunctions()
{
   //Get Function factory.
   FunctionFactory& oFactory = GetFunctionFactory();
   //Create functions from the factory and use.
}

FunctionFactory& GetFunctionFactory()
{
   //Get shared object handle.
   void* pHandle = dlopen("someso.so");

   //Get function ptr for Factory getter.
   typedef FunctionFactory* (*tpfFacGet)();
   tpfFacGet pF = static_cast<tpfFacGet>(dlsym(pHandle, "GetFactory"));

   //Call function and return object.
   return *((*pF)());
}

Now, it’s easy to see that loads of stuff can go wrong. If I did it like I always do, I’d return pointers instead of references, and I’d check if they were NULL and print an error message and get out if they weren’t. That way, I know where things went wrong and I can even try to recover from that (i.e. If I successfully load the factory and fail to load just a single function, I may still continue). What I don’t understand is how to use exceptions in such a scenario and how to recover the program rather than printing an error message and qutting. Can someone tell me how I am to do this in C++ish way?

  • 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-18T05:00:14+00:00Added an answer on May 18, 2026 at 5:00 am

    We don’t even need return codes. If a problem occurs it should be in the exception.

    int main()
    {
        try
        {
            LoadFunctions();
            // if we're here, everything succeeded!
        }
        catch(std::exception _e)
        {
            // output exception message, quit gracefully
        } 
    
        // IRRESPECTIVE OF SUCCESS/FAILURE WE END UP HERE
    
        return 0;
    } // eo main
    

    EDIT:

    Okay, so lets say that you have an alternative method of loading functions should LoadFunctions() fail. You might be tempted to call that in the catch handler, but this way you’ll quickly end up with a huge amount of nested exception handlers which just complicates things.

    So now we get down to the question of design. LoadFunctions should succeed if functions are loaded and throw out an exception if it does not. In this hypothetical example of an alternative method of loading functions, that call should be within the LoadFunctions method. This alternative method does not need to be visible to the caller.

    At the top level we either end up with functions, or we do not. Writing good exception handling, in my opinion is about getting rid of grey areas. The function did what it was told to do, or it didn’t.

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

Sidebar

Related Questions

Can I order my users in the database, so I don't have to say
Can I change the field public virtual ClassOne ClassOne { get; set; } to
Can any one tell, how to get the result of LINQ query contains group
I have a jquery bug and I've been looking for hours now, I can't
Can't figure out how to do this in a pretty way : I have
Can someone help me with this scenario? *There is a button, which when tapped,
Can't seem to figure out what's wrong with the simple getJSON call below. It's
I have an autohotkey script which looks up a word in a bilingual dictionary
Can anyone give me an example of how to return the following json simply
I have an array which has BIG numbers and small numbers in it. 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.