should we call exit() inside exceptional catching blocks

L

lovecreatesbea...

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*/
 
A

Alexander Block

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*/

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.
 
L

lovecreatesbea...

, why do you catch it here? You should have a try/catch

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

//...
} catch (exception &e){
//...
 
A

Alexander Block

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

//...} catch (exception &e){

//...

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.
 
L

lovecreatesbea...

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)

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.
... that catches all uncaught exceptions.

But still place some special exception handling code for special non-
main() function? Doesn't this conflicts with the rule above?
This allows all
destructors in the stack to be called and makes it possible to
gracefully clean up everything.

Does this relate to object scope things?
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top