Can I have nested try-catch blocks? For example:
void f() { try { //Some code try { //Some code } catch(ExceptionA a) { //Some specific exception handling } //Some code } catch(...) { //Some exception handling } }//f
Yes, we can declare a try-catch block within another try-catch block, this is called nested try-catch block.
No need for nested or multiple try-catches inside the same method. Code like the one you showed does indeed not make any sense in most if not all cases.
If the exception thrown from func2() in the inner try block is type_err , the program skips out of both try blocks to the second catch block without invoking func3() , because no appropriate catch block exists following the inner try block. You can also nest a try block within a catch block.
In C#, the nesting of the try & catch block is allowed. The nesting of try block means one try block can be nested into another try block.
Yes perfectly legal.
Though it would be best to move inner ones into another method so it looks cleaner and your method(s) are smaller
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With