0

how can I do that ?

 void x()
     {....
        if (...)
        {try
            {}
            catch (ComException com)
                { throw com}
            finally  // in any case, executed fine!
                {...instructions.......}

        }
        ... instructions...// not executed in case of exception because the finally can't embrace the following code too... but this block of code needs to be executed in any case too...
        {}


     }
KitAndKat
  • 953
  • 3
  • 14
  • 29
  • Why are you throwing an exception in the catch block? – Matthew Jones Apr 09 '10 at 21:21
  • @Matthew Jones I'm guessing the OP is omitting some logging code within the catch block. Otherwise yes, it's pointless to catch and rethrow. – Kevin Pang Apr 09 '10 at 21:33
  • because I have to catch a special exception , the Com one, and then throw it to the calling method, maybe the syntax could be lightened... (im in c#).... but no there is no other code in the catch but throw.... otherwise how would you write it ? – KitAndKat Apr 09 '10 at 21:34
  • 1
    @Anna: What's the point in catching an exception and than throwing it again? Other exceptions could still be thrown, since you don't catch them (you'd have to add `catch (Exception ex)` as last `catch` to do so). And when re-throwing an exception use `throw;`, not `throw ex;`. This keeps the call-stack and line-numbers intact. – Ronald Apr 09 '10 at 21:46
  • the point is that I need this exception to be thrown but I need the finally for the code to go on. So how I am supposed to use finally without a catch syntactically I mean? – KitAndKat Apr 09 '10 at 21:55
  • @Anna: You can have a `finally` block without having a `catch` block. – dtb Apr 10 '10 at 23:45

4 Answers4

3

That's incorrect logic. The else block will not be executed if the code goes into the if statement.

If you really need it to be executed even in case of exception, copy the code from the else block into the finally block.

EDIT: So I think what you want is this:

try
{
     if()
     {
           try
           {
               //Code
           }
           catch(ComException e)
           {
                throw e;
           }
     }
}
finally
{
    /*....instructions.....*/
}

The reasoning behind this is that the inner try will execute the code if the IF statement is true, and will catch and then re-throw the ComException if it encounters it. The code in the finally block will execute regardless of either the IF statement or the catching of a ComException.

Does this explain the position better?

With apologies to dtb; he answered this first, I just added an explanation.

Community
  • 1
  • 1
Matthew Jones
  • 25,644
  • 17
  • 102
  • 155
2

Move the code in the "else" branch to a separate method. Then call that method from both the "else" and the "finally".

David
  • 34,223
  • 3
  • 62
  • 80
  • this was the only thing to do. but now I learned that when you need to do things that are really far-fetched, it might be due to some weakness in the functional and conceptual model of your code... so shame on me! – KitAndKat Apr 13 '10 at 22:26
2

Are you looking for this?

try
{
    if (...)
    {
        try
        {
            ...
        }
        catch (ComException)
        {
            ...
        }
    }
}
finally
{
    ...
}

The finally block is executed regardless of whether the condition holds or not.

dtb
  • 213,145
  • 36
  • 401
  • 431
  • the problem in this solution is I can't target a specific instruction with my try but I have to embrace a lot of instructions whereas I want a specific instruction to be caught in the calling method... – KitAndKat Apr 09 '10 at 21:29
0

If something needs to be executed, it must go in the finally block. Finally always executes, no matter what happens in try and catch blocks. The context of the "else" is really outside your try/catch/finally segment.

Cylon Cat
  • 7,111
  • 2
  • 25
  • 33