should we call exit() inside exceptional catching blocks

Discussion in 'C++' started by lovecreatesbea..., Mar 26, 2007.

  1. Is the following code (without any code at lines x and x + 3) correct?
    Is it better to call exit() or re-throw the exceptions at line x and
    line x + 3?. What is the better code should we place at line x and
    line x + 3?

    try {
    cnn = env->createConnection(user, pwd, db);
    } catch (SQLException &esql){
    cerr << "DB Exception: " << esql.getMessage();
    /* line x ? */
    } catch (exception &e){
    cerr << "Exception: " << e.what();
    /* line x + 3 ? */

    /*more code*/
    lovecreatesbea..., Mar 26, 2007
    1. Advertisements

  2. If it's a critical exception that stops the execution of your
    application, why do you catch it here? You should have a try/catch
    statement in your lowest application level (your main function for
    example) that catches all uncaught exceptions. This allows all
    destructors in the stack to be called and makes it possible to
    gracefully clean up everything.
    Alexander Block, Mar 26, 2007
    1. Advertisements

  3. Do you mean the second try/catch block in my previous post?

    } catch (exception &e){
    lovecreatesbea..., Mar 26, 2007
  4. I mean both. Having a rethrowing exception handler makes only sense if
    you need to do special clean up (deallocating buffers for example).

    Using exit() results in an abrupt exit. No destructors will be called
    after that.
    Alexander Block, Mar 26, 2007
  5. Thank you. Please correct me if I misunderstand the knowledge.

    Placing exception handling code in the main() function and without
    placeing them in other called functions makes the code more cleaner
    and simpler.
    But still place some special exception handling code for special non-
    main() function? Doesn't this conflicts with the rule above?
    Does this relate to object scope things?
    lovecreatesbea..., Mar 27, 2007
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.