C++ exception handling

Discussion in 'C++' started by Nikos Hatzigiannakis, Dec 16, 2007.

  1. I am trying to understand how to use an exception handler to heal from a
    given exception, and not just display a message!



    Since an exception is raised the control of the program is transferred to
    the proper catch clause. What can we do after displaying a message to
    continue the normal operation of the program?
     
    Nikos Hatzigiannakis, Dec 16, 2007
    #1
    1. Advertising

  2. On 2007-12-16 17:16, Nikos Hatzigiannakis wrote:
    > I am trying to understand how to use an exception handler to heal from a
    > given exception, and not just display a message!
    >
    >
    >
    > Since an exception is raised the control of the program is transferred to
    > the proper catch clause. What can we do after displaying a message to
    > continue the normal operation of the program?


    It of course depends on why there was an exception to begin with. If the
    exception was raised due to a transient error then you can retry:

    bool succeed = false;

    while (!succeed)
    {
    try
    {
    doSomelthingThatMightThrow()
    succeed = true;
    }
    catch(SomeException& e)
    {
    logFailure();
    }
    }

    In other cases you might try a different approach:

    int result;
    try
    {
    result = firstApproach();
    }
    catch(Exception& e)
    {
    result = secondApproach();
    }

    There is no simple answer, since it all depends on what you are trying
    to do and the reasons for the exceptions.

    --
    Erik Wikström
     
    Erik Wikström, Dec 16, 2007
    #2
    1. Advertising

  3. Nikos Hatzigiannakis

    Guest

    Hi,
    Take a look at:
    "C++ How to Program, Fifth Edition" By Dietel & Dietel.
    Chapter 16... Exception Handling.
     
    , Dec 16, 2007
    #3
  4. Any links to related content ?
    ? <> ?????? ??? ??????
    news:...
    > Hi,
    > Take a look at:
    > "C++ How to Program, Fifth Edition" By Dietel & Dietel.
    > Chapter 16... Exception Handling.
     
    Hatzigiannakis Nikos, Dec 17, 2007
    #4
  5. On 2007-12-17 12:53, Hatzigiannakis Nikos wrote:
    > Any links to related content ?
    > ? <> ?????? ??? ??????
    > news:...
    >> Hi,
    >> Take a look at:
    >> "C++ How to Program, Fifth Edition" By Dietel & Dietel.
    >> Chapter 16... Exception Handling.


    www.amazon.com

    --
    Erik Wikström
     
    Erik Wikström, Dec 17, 2007
    #5
  6. On 2007-12-17 00:34, wrote:
    > Hi,
    > Take a look at:
    > "C++ How to Program, Fifth Edition" By Dietel & Dietel.
    > Chapter 16... Exception Handling.


    If you want to reply to the OP then please reply to the OP and to my
    reply to the OP. Also please quote the text you are replying to.

    --
    Erik Wikström
     
    Erik Wikström, Dec 17, 2007
    #6
  7. Nikos Hatzigiannakis

    john Guest

    Nikos Hatzigiannakis wrote:
    >
    > I am trying to understand how to use an exception handler to heal from a
    > given exception, and not just display a message!


    Can you become more specific to yoir issue? There are many ways you can
    handle exceptions, for example you can place the entire body of a
    function in a try block, like this:


    void g() try
    {
    // function body
    }

    catch(const exception &e)
    {
    // Do stuff
    }




    > Since an exception is raised the control of the program is transferred to
    > the proper catch clause. What can we do after displaying a message to
    > continue the normal operation of the program?


    You can retry the operation or whatever.
     
    john, Dec 17, 2007
    #7
  8. Nikos Hatzigiannakis

    Kira Yamato Guest

    On 2007-12-16 14:10:50 -0500, Erik Wikström <> said:

    >
    > bool succeed = false;
    >
    > while (!succeed)
    > {
    > try
    > {
    > doSomelthingThatMightThrow()
    > succeed = true;
    > }
    > catch(SomeException& e)
    > {
    > logFailure();
    > }
    > }


    A bit off topic, but must you utilize the flag variable succeed? That
    approach feels so C-like.

    Rather, how about the following version?

    while(1)
    try
    {
    doSomething();
    break;
    }
    catch(SomeException &e)
    {
    fixProblem();
    }

    >
    > In other cases you might try a different approach:
    >
    > int result;
    > try
    > {
    > result = firstApproach();
    > }
    > catch(Exception& e)
    > {
    > result = secondApproach();
    > }


    Err... For multiple approaches, I rather recommend the following
    more-symmetrical structure instead:

    for(;;)
    {
    try { approach1(); break; }
    catch(SomeException &e) { logFailure(); }

    try { approach2(); break; }
    catch(SomeException &e) { logFailure(); }

    try { approach3(); break; }
    catch(SomeException &e) { logFailure(); }

    throw AllAproachFailedException();
    }

    --

    -kira
     
    Kira Yamato, Dec 17, 2007
    #8
  9. Nikos Hatzigiannakis

    john Guest

    Consider the following working example:


    #include <iostream>
    #include <exception>

    inline void g() try
    {
    throw std::exception();
    }


    catch(const std::exception &)
    {
    std::cout<< "Exception received!\n";
    g();
    }


    int main()
    {
    g();
    }
     
    john, Dec 18, 2007
    #9
  10. Nikos Hatzigiannakis

    Kira Yamato Guest

    On 2007-12-17 20:25:53 -0500, john <> said:

    > Consider the following working example:
    >
    >
    > #include <iostream>
    > #include <exception>
    >
    > inline void g() try
    > {
    > throw std::exception();
    > }
    >
    >
    > catch(const std::exception &)
    > {
    > std::cout<< "Exception received!\n";
    > g();
    > }
    >
    >
    > int main()
    > {
    > g();
    > }


    Neat. The syntax shows elegantly that it can do re-tries.

    It would've been practical too if C++ supports tail-recursion.

    --

    -kira
     
    Kira Yamato, Dec 18, 2007
    #10
  11. Nikos Hatzigiannakis

    Jorgen Grahn Guest

    On Sun, 16 Dec 2007 18:16:30 +0200, Nikos Hatzigiannakis <> wrote:
    > I am trying to understand how to use an exception handler to heal from a
    > given exception, and not just display a message!


    > Since an exception is raised the control of the program is transferred to
    > the proper catch clause. What can we do after displaying a message to
    > continue the normal operation of the program?


    The same things as if you passed an error code back instead of
    throwing an exception.

    The main difference is, I guess, if you use exceptions you can design
    your code to handle cleanup automatically, as the exception floats up
    the call chain towards the catch clause.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
    \X/ snipabacken.dyndns.org> R'lyeh wgah'nagl fhtagn!
     
    Jorgen Grahn, Dec 18, 2007
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Babu
    Replies:
    1
    Views:
    2,919
    Jim Gibson
    Mar 10, 2005
  2. Mark Tarver
    Replies:
    22
    Views:
    1,313
    J Kenneth King
    Apr 26, 2009
  3. Peter
    Replies:
    34
    Views:
    1,941
    James Kanze
    Oct 17, 2009
  4. VSK
    Replies:
    0
    Views:
    248
  5. Iñaki Baz Castillo
    Replies:
    1
    Views:
    190
    Iñaki Baz Castillo
    Apr 15, 2008
Loading...

Share This Page