Jump to content
  • Advertisement
Sign in to follow this  
redshock

I made a working alternative Finally block for C++ exceptions. Opinions welcomed.

This topic is 1065 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

At work, I'm used to exceptions with the try-catch-finally format. I wondered how to add 'finally' to C++.

 

After seeing a finally block wasn't implemented in C++11 (Stroustrup is against them in favor of RAII...), and how others asked for it, I tried this alternative. I think it does the trick without any issue, as finally is used to clean up any resources allocated in the try block. I think it's nice to have. Do you think this alternative is just as effective?

 

1) Put this at the top of your code: #define finally

 

2) And, since you can create a unit block using { }, just put finally before that.

 

Example (poor, but proves a point):

#define finally#include using namespace std;int main(){         int *p = NULL;    try    {        cout << "Allocating..." << endl;        p = new int;    }    catch(bad_alloc e)    {        cout << e.what() << endl;    }    finally {        cout << "Cleaning up!" << endl;        delete p;    }    return 0;}

Output:

Allocating...

Cleaning up!

Edited by jbadams
Restored deleted post contents from history.

Share this post


Link to post
Share on other sites
Advertisement

It runs for me. I added the throw/catch exception.

#define finally#include using namespace std;int main(){         int *p = NULL;    try    {        cout << "Allocating." << endl;        p = new int;        throw bad_alloc();    }    catch(bad_alloc e)    {        cout << e.what() << endl;    }      catch (exception e)    {        cout << e.what() << endl;    }    finally    {        cout << "Done!" << endl;        if (p) {            delete p;            p = NULL;        }    }    system("pause");    return 0;}

Current Output: Allocating.

bad allocation

Done!

Edited by jbadams
Restored deleted post contents from history.

Share this post


Link to post
Share on other sites
This thread is dumb.
 
#define wait_no

void is_thread_dumb() {
    try {
        throw std::exception("yes");
    } catch ( const std::exception& ex ) {
        throw ex;
    } wait_no {
        cout << "nope\n";
    }
}

int main() {
    try {
        is_thread_dumb();
    } catch ( const std::exception& ex ) {
        std::cout << ex.what();
    }
}
Output: "yes"


And here I was hoping for some magic C++ trickery. What a disappointment. Edited by fastcall22

Share this post


Link to post
Share on other sites

What do you think 'finally' does?

 

This works as an alternative. When I asked for opinions, I didn't expect an immature person calling it 'dumb' or reputation clickers who can't voice an opinion. This site is filled with kids with nothing better to do. Go outside. Learn to socialize and be respectable.

Edited by redshock

Share this post


Link to post
Share on other sites

If you really want to have a safe finally implementation (even though I consider it more or less useless due to destructors and RAII as already mentioned), I think you can use c++11 lamdas and use said destructor behaviour:

class Finally
{
public:

    using FinallyFunc = void (*)();
    
    Finally(FinallyFunc finally) : m_finally(finally)
    {
    }
    
    ~Finally(void)
    {
        m_finally();
    }
    
private:
    
    std::function<void ()> m_finally;
};

{
    const Finally finally([] ()
    {  
        std::cout << "Will always be printed";
    });

    try
    {
        throw new std::exception;
    }
    catch(...)
    {
        return;
    }
}

std::cout << "Will never be printed";

I didn't use lamdas myself but I suppose this should work if you call the lamda in the "Finally" destructor. Syntax is a bit strange since you have to put { } around the whole try-catch block and have to place the finally declaration on top. I probably wouldn't use this personally but if you really insist (and have access to c++11).

Edited by Juliean

Share this post


Link to post
Share on other sites

When I asked for opinions, I didn't expect an immature person calling it 'dumb' or reputation clickers who can't voice an opinion. This site is filled with kids with nothing better to do. Go outside. Learn to socialize and be respectable.


We strive for a higher level of civility on these boards. This thread is closed.

Share this post


Link to post
Share on other sites

This topic is 1065 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!