2

I think the title explain whole my question.

I'd like to know, where I execute my code on finally statement, if it come from a try or a catch.

Is it possible?

markzzz
  • 47,390
  • 120
  • 299
  • 507
  • 1
    As the last statement in your try (or in the catch) just set a boolean value. – George Duckett Apr 19 '13 at 09:18
  • 2
    Wouldn't it be better to just put the code either in the try or the catch? What's your concrete case? – devoured elysium Apr 19 '13 at 09:19
  • @devouredelysium: One situation I've used this approach in: in the finally block, I want to roll back a transaction if anything went wrong, but not if the operation was actually successful. (The last real action of the try block would be the commit.) This can *sometimes* be mitigated by putting the "don't reverse things" logic into the transaction object itself, but that's not always an option. – Jon Skeet Apr 19 '13 at 09:23
  • But why wouldn't you put that logic in the catch? – devoured elysium Apr 19 '13 at 09:45
  • @devouredelysium: Because that requires you to *have* a `catch` block for that `Exception`. What if it's not an exception you'd anticipated? Do you have a `catch (Exception e)` block for all try blocks, just in case there's an exception you weren't aware of? You still definitely want to clean up in that situation, but then just rethrow... which is more cleanly done in the finally block, IMO. – Jon Skeet Apr 19 '13 at 09:49

1 Answers1

11

Well, the simplest approach is:

bool success = false;
try
{
    ...
    // This should be the last statement in the try block
    success = true;
}
catch(...)
{
    ...
}
finally
{
    if (success)
    {
        ...
    }
}

This isn't a specific language feature - it's just using the fact that other than really remarkable situations (asynchronous exceptions, basically), you're not going to get an exception occurring after the assignment to success.

Note that success being false doesn't mean that any of your catch blocks have executed - it could be that an exception was thrown which you're not catching (or that you returned before the end of the try block). Basically it only indicates "reached end of try block" - which is normally all that's needed.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • it says the variable it is not assigned :O – markzzz Apr 19 '13 at 09:52
  • @markzzz: Are you sure you included the `= false` assignment before the `try` block? If you take the exact code I've given, it should be fine. – Jon Skeet Apr 19 '13 at 09:55
  • how can I paste it to you? – markzzz Apr 19 '13 at 09:57
  • @markzzz: Edit your question, or use pastebin.com. But as I say, if you're talking about the `success` variable then it clearly *is* assigned in my code - and if you're talking about another variable then you're asking me to be psychic, which is tricky... – Jon Skeet Apr 19 '13 at 09:58
  • The only difference is I have `if (txtMonitor.InvokeRequired && success)` – markzzz Apr 19 '13 at 09:59
  • @markzzz: Well that doesn't even use the `success` variable, and we've no idea where those two variables are declared. Put yourself in my position, who can't see any of your code: how much chance do you think I've got of diagnosing what's wrong on the basis of just what you've told me? – Jon Skeet Apr 19 '13 at 10:00
  • @markzzz: as Jon suggested, you better edit your question and be more explicit (show your code). – L-Four Apr 19 '13 at 10:03
  • Well, changing to `if(success && txtMonitor.InvokeRequired)` :O That's really strange :) – markzzz Apr 19 '13 at 10:06
  • 3
    @markzzz: Poor show on changing your comment after I'd replied to it, making my comment look crazy (your variable was `folderCopied` rather than `success` when I added the comment). And without seeing the code which was apparently broken in context, it's very hard to work out what was going on. – Jon Skeet Apr 19 '13 at 10:07
  • I changed it in some seconds :) But ok now I understand the trouble, and I fix it ;) Just strange that `if(success && txtMonitor.InvokeRequired)` works and `if (txtMonitor.InvokeRequired && success)` won't, isnt it? – markzzz Apr 19 '13 at 10:17