10

Is there any possibility to catch inner exception:

try 
{
    ttsbegin;    
    info("step one");        
    try 
    {
       info("step two");
       throw Error("error");
    }
    catch 
    {
       info("catch step two");
    }        
    ttscommit;
}
catch 
{
    info("catch step one");
    ttsabort;
}

I know I can get it commenting ttsbegin; / ttscommit, but I need to have a transaction.

ztirom
  • 4,382
  • 3
  • 28
  • 39
ceth
  • 44,198
  • 62
  • 180
  • 289
  • http://stackoverflow.com/questions/183499/is-there-a-preference-for-nested-try-catch-blocks this might shed some light... – Frank Tudor Sep 21 '12 at 18:22
  • 1
    http://msdn.microsoft.com/en-us/library/aa893385.aspx 'Sample 5' under 'Exception Handling'. Sorry about that. – Frank Tudor Sep 21 '12 at 18:34
  • Sample 5 uses transaction in the inner try..catch block. It is not my case. Moreover, if I add inner transaction in my example (but left outer) - nothing changed: i'll get "catch step one" message. – ceth Sep 21 '12 at 19:24

1 Answers1

11

No, it is not possible (unless your exception is UpdateConflict or DuplicateKeyException).

The documentation states:

If an exception is thrown inside a transaction, the transaction is automatically aborted (a ttsAbort operation occurs). This applies both for exceptions thrown manually and for exceptions thrown by the system.

When an exception is thrown inside a ttsBegin - ttsCommit transaction block, no catch statement inside that transaction block can process the exception. Instead, the innermost catch statements that are outside the transaction block are the first catch statements to be tested.

The logic is: 1) your transaction is aborted by the throw 2) then you cannot possible recover from that inside your transaction 3) hence take the innermost catch outside the transaction.

The two exceptions (pun intended) are UpdateConflict and DuplicateKeyException which do not make a ttsabort and hence may be caught inside the transaction.

Also see this blog entry which demonstrate that.

Update: Potential pitfall

Using catch all (no exception type specified) can cause problems. See this blog post.
As of D365O update 5 the the two exceptions are not caught by a catch all if the tts level is greater than one. See this blog post.

Community
  • 1
  • 1
Jan B. Kjeldsen
  • 17,817
  • 5
  • 32
  • 50