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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T17:01:17+00:00 2026-05-14T17:01:17+00:00

I’m trying to decide between two ways of instantiating an object & handling any

  • 0

I’m trying to decide between two ways of instantiating an object & handling any constructor exceptions for an object that is critical to my program, i.e. if construction fails the program can’t continue.

I have a class SimpleMIDIOut that wraps basic Win32 MIDI functions. It will open a MIDI device in the constructor and close it in the destructor. It will throw an exception inherited from std::exception in the constructor if the MIDI device cannot be opened.

Which of the following ways of catching constructor exceptions for this object would be more in line with C++ best practices

Method 1 – Stack allocated object, only in scope inside try block

#include <iostream>
#include "simplemidiout.h"

int main()
{
    try
    {
        SimpleMIDIOut myOut;  //constructor will throw if MIDI device cannot be opened
        myOut.PlayNote(60,100);

        //.....
        //myOut goes out of scope outside this block
        //so basically the whole program has to be inside 
        //this block.
        //On the plus side, it's on the stack so 
        //destructor that handles object cleanup
        //is called automatically, more inline with RAII idiom?
    }
    catch(const std::exception& e)
    {
        std::cout << e.what() << std::endl;
        std::cin.ignore();
        return 1;
    }

    std::cin.ignore();
    return 0;   
}

Method 2 – Pointer to object, heap allocated, nicer structured code?

#include <iostream>
#include "simplemidiout.h"

int main()
{
    SimpleMIDIOut *myOut;

    try
    {
        myOut = new SimpleMIDIOut();
    }
    catch(const std::exception& e)
    {
        std::cout << e.what() << std::endl;
        delete myOut;
        return 1;
    }

    myOut->PlayNote(60,100);

    std::cin.ignore();

    delete myOut;
    return 0;

}

I like the look of the code in Method 2 better, don’t have to jam my whole program into a try block, but Method 1 creates the object on the stack so C++ manages the object’s life time, which is more in tune with RAII philosophy isn’t it?

I’m still a novice at this so any feedback on the above is much appreciated. If there’s an even better way to check for/handle constructor failure in a siatuation like this please let me know.

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

    Personally, I prefer the first style you’ve used – Method 1 – that of allocating the SimpleMIDIOut object as local to the scope of the try-catch block.

    • For me, one of the benefits of a try-catch block is that is provides a neat, tidy place for that error handling code – the catch block – that allows you to specify your business logic in one nice, readable, unbroken flow.

    • Secondly, the try-catch block you’ve specified is generic enough to deal with any exception that derives from std::exception – so having the bulk of your program code within the try-catch block isn’t a bad thing. It’ll prevent your program from terminating unexpectedly if the worst happens and an exception gets thrown. Potentially that could be an exception that you don’t expect, like an index out of bounds, or a memory allocation exception.

    • If you dislike having lots of code within the try-catch block for readability reasons, that’s OK, because it’s good design practice to refactor big lumps of code into functions. By doing this, you can keep the main function itself to a minimal number of lines.

    Looking at Method 2:

    • You don’t need that delete in the catch block. If an exception was thrown during construction then there’s no object there to delete anyway.

    • Have you considered how you plan to cater for any std::exception that could be thrown by myOut->PlayNote? It’s outside the scope of your try-catch, so an exception here would kill the program unexpectedly. This is what I was getting at with my second bullet, above.

    If you were to decide to wrap most of the program in that try-catch block, but would still like to dynamically allocate the SimpleMIDIOut object, you could make the memory management a bit easier by using an auto_ptr to manage the memory for you in the event of an exception:

    try
    {
        std::auto_ptr<SimpleMIDIOut> myOut(new SimpleMIDIOut());
        myOut->PlayNote(60,100);
        std::cin.ignore();
    } // myOut goes out of scope, SimpleMIDIOut object deleted
    catch(const std::exception& e)
    {
        std::cout << e.what() << std::endl;
        return 1;
    }
    
    
    return 0;
    

    …but you may as well just create the SimpleMIDIOut object as local rather than dynamic.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've tracked down a weird MySQL problem to the two different ways I was
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:

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.