Nested Try Blocks in C++ Last Updated : 28 Nov, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, a nested try block refers to the try-block nested inside another try or catch block. It is used to handle exceptions in cases where different exceptions occur in a different part of the code. Syntax of Nested Try Blocks The nested try/catch takes this syntax: try { // Code...... throw e2 try { // code..... throw e1 } catch (Exception e1) { // handling exception } } catch (Exception e2) { // handling exception } Here, e1: Exception thrown in inner block.e2: Exception thrown in outer block.Example of Nested Try Blocks C++ // C++ program to illustrate the use of nested try blocks #include <iostream> using namespace std; // function throwing exceptions void func(int n) { if (n < 10) { throw 22; } else { throw 'c'; } } // driver code int main() { try { try { cout << "Throwing exception from inner try " "block\n"; func(2); } catch (int n) { cout << "Inner Catch Block caught the exception" << endl; } } catch (char c) { cout << "Outer catch block caught the exception" << endl; } cout << "Out of the block"; return 0; } OutputThrowing exception from inner try block Inner Catch Block caught the exception Out of the block Explanation Here, we used func() function to throw two exceptions of int and char type. We used an inner try block to catch integer exceptions. Now, whenever the try blocks throw an exception, the control moves outwards from the nested block till the matching catch block is found. In this case, it was the inner catch block that caught the exception. What happens if we throw a character exception that the outer catch block is programmed to handle? Let's see, C++ // C++ program to illustrate the use of nested try blocks #include <iostream> using namespace std; // function throwing exceptions void func(int n) { if (n < 10) { throw 22; } else { throw 'c'; } } // driver code int main() { try { try { cout << "Throwing exception from inner try " "block\n"; func(12); } catch (int n) { cout << "Inner Catch Block caught the exception" << endl; } } catch (char c) { cout << "Outer catch block caught the exception" << endl; } cout << "Out of the block"; return 0; } OutputThrowing exception from inner try block Outer catch block caught the exception Out of the block Here, the outer catch block caught the exception as expected. The try blocks can also be the nested in the catch block in a similar way. Comment More infoAdvertise with us Next Article Try and Catch Block in MATLAB R rohitsharma_26 Follow Improve Article Tags : C++ Geeks Premier League Geeks Premier League 2023 Practice Tags : CPP Similar Reads Blocks in Objective-C Blocks in Objective-C are a potent tool that can simplify the development process. Blocks are an alternative to traditional functions, allowing you to write code that is more concise and efficient. They are used for many different purposes, including as callbacks and iterators. Blocks can also be us 6 min read Nested if in C++ If is a type of condition checking in which a condition turns out to be true a block of statements is executed. Syntax: // if base_condition is true // every inside the { } block will be executed if (base_condition) { statement 1............... statement 2 .............. } Example C++ // C++ Program 3 min read Try and Catch Block in MATLAB Exception Handling is a mechanism used to resolve/handle Runtime errors that arise during the execution of a program. Most programming languages offer some level of exception handling to deal with errors that arise while the program is executing. Exception handling deals with these events to avoid t 3 min read Nested Classes in C++ A nested class is a class which is declared in another enclosing class. A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules shall be obeyed. For example, p 2 min read Try Catch Block in Programming In programming, a try catch block is used for exception handling. The try block contains code that might throw an exception and the catch block handles specific exceptions by providing custom code. It prevents program termination when exceptions occur. Remember, we can use a try block without a catc 7 min read C++ Nested if-else Statement Nested if-else statements are those statements in which there is an if statement inside another if else. We use nested if-else statements when we want to implement multilayer conditions (condition inside the condition inside the condition and so on). C++ allows any number of nesting levels.Let's tak 3 min read Like