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?