Nested Try Catch not working properly

In a nested try catch, the inner “catch” happens first, is my understanding, but can you please correct me if I am wrong?

I want the inner “catch” block to happen first, rather than the outer “catch” block, but for some reason, my program is running the outer “catch” even though an exception is happening in the inner “try” block. I don’t understand what is going on. It looks like this.

Outer - Try {
… // More code here
… … Inner - Try {
… … … … //Do action XXX
… … }
… … Inner - Catch {
… … … … //Do alternative action YYY
… … }
… // More code here
}
Outer - Catch
{
… … throw: “error message from outer catch block”
}

I am using the inner try catch, so that if action XXX fails, it does action YYY. We know for sure one of them works, but we don’t know which one works until we actually try running the program, so I did this.

However, for some reason, when action XXX fails, it just leaves the inner try-catch, and it throws “error message from outer catch block”!
Why does this happen? I thought in this code, action YYY happens? It doesn’t even go to the inner catch block, so action YYY never happens.

My understanding was, when action XXX fails, it runs action YYY. Even action YYY fails, it will throw an exception, which will be caught by the outer “catch” block. But I guess I am wrong.

Can someone help me understand this?

Hello !,
Are you executing it o debug mode?

3 Likes
  1. Did you check if the exception happened outsite Inner-Try/Catch?
  2. In outer catch, you should try to write out exception.tostring() then check if it’s XXX’s exception
1 Like

No, I executed it in Run mode.

But I added Log Message activity in all of the Try blocks and Catch blocks, but the result shows that the execution first tries action XXX, and when it fails, it won’t go into the inner catch block at all.

I wrote my code like this.

Outer-Try{
… … Inner-Try{
… … … Log Message: Message A
… … … Action XXX
… … }
… … Inner-Catch{
… … … Log Message: Message B
… … … Action YYY
… … }
}
Outer-Catch{
… … Log Message: Message C
}

When I run this, the messages I get are: Message A and Message C.
This means, when the inner-Try fails, it next executes Outer-Catch! Why is it not executing Inner-Catch? This doesn’t make sense.

Thank you. I actually figured it out when I tried running in debug mode.

Can you share the reason?

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.