Menu

Rethrow the exception

drakon
2011-10-04
2013-04-26
  • drakon

    drakon - 2011-10-04

    Can I use CException to generate an Exception inside the Try-Catch block?

    Something like that:

    Try {
        foo();
    } Catch(e) {
        Throw(ERROR);
    }
    
     
  • Mark VanderVoord

    Yes, you can absolutely do this.  In fact, I often use it to clean things up (release a semaphore, etc) before rethrowing an error, like so:

    Try {
      foo();
    }
    Catch(e) {
      cleanup();
      Throw(e);
    }
    

    Then your error will get thrown to the next nested Catch.  :)

     
  • drakon

    drakon - 2011-10-04

    It doesn't work for me.

    Try {
      foo();
    } Catch(e) {
      foo2();
     Throw(e); 
    }
    

    Foo2 is called in the loop or breaks with SIGSEGV. Maybe it has something todo with threads, because I use all my exception inside one thread and haven't changed CEXCEPTION_NUM_ID and CEXCEPTION_GET_ID

     
  • Mark VanderVoord

    If all of your Try's and Throws are within a single thread/task/etc… you should be fine.  You only need to define those macros if there is a chance of getting a mismatch (a throw in one thread should not be caught by another thread, etc). 

    I'm making an assumption that you have a Try…Catch somewhere around the one that you are listing (nested directly outside or around this function when it gets called, etc).  If not, your error is that your second throw never gets caught anywhere.  There's not a global catch anywhere to grab it for you.

    If both of those items are okay, then I'll need a bit more detail to help you out.  Can you paste in a more complete example that reproduces the problem?  What compiler are you using?

    Mark

     
  • drakon

    drakon - 2011-10-05

    My function:

    int i;
    for(i=0; <= max; i++) {
      Try {
        foo();
        break;
      } Catch(e) {
        if(i==max) {
          foo2();
          Throw(e);
        }
      } 
    }
    

    I removed all threads from my application and foo2 is still called in the loop. I just can't understand how it could be possible.

     
  • drakon

    drakon - 2011-10-05

    Compiler:

    gcc version 4.2.0 20070413 (prerelease) (CodeSourcery Sourcery G++ Lite 2007q1-10)

     
  • drakon

    drakon - 2011-10-05

    I found the answer in the documentation! It is not allowed to use the return statement(or break in my case).

     
  • Mark VanderVoord

    :)  Yes, you found it.

     

Log in to post a comment.