try catch

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MrPickle
    New Member
    • Jul 2008
    • 100

    #1

    try catch

    With try catch blocks what happens when multiple exceptions are thrown?

    Is there chance for multiple exceptions or does it stop as soon as one is thrown?

    eg;

    Code:
    try
    {
        foo(); //throws an exception
        bar(); //Will this function be executed? If so what happens if it throws an exception?
    }
    catch(...)
    {
       //...
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    If foo throws an exception that is catchable by the catch block the execution switches to the catch block, bar is never executed.

    If foo throws an exception that is not catchable by the catch block then stack-unwinding happens. That is the exception is passed up through the current call stack until either a catch block for the exception is found or main is exited at which point the program terminates with the exception, again bar is never executed.

    Comment

    • RRick
      Recognized Expert Contributor
      • Feb 2007
      • 463

      #3
      In a single process (I think) only one exception can be thrown at a time. The reason for this is because of the code execution described above by Banfa. No other exception can be created because the only code being executed is the code for the original exception.

      That sounds reasonable, but what happens in a threaded environment? Since any number of threads can be running concurrently, any number of exceptions could be thrown. But the same logic for having a single exception per process also applies to threads. Each thread processes a single exception at a time and when the exception goes beyond the scope of the thread, the thread is terminated. I don't believe that will cause the entire program to be stopped.

      Comment

      Working...