- Catch – a program that utilises an exception handler to catch an Exception. It is added to the part of a program where you need to deal with the error.
- Try – the try block recognises the code block for which certain exceptions will be enacted. It ought to be followed by one/more catch blocks.
How try-catch in c++ works?
Try catch in c++ is defined as the exception that is raised in the code block.The exception will be gotten by a strategy utilising try and catch keywords. The try/catch block should encompass code that may throw an exception. Such code is known as protected code.
Try/catch Syntax:
The try/catch takes this syntax:
try { // the protected code } catch( Exception_Name exception1 ){ // catch block } catch( Exception_Name exception2 ){ // catch block } catch( Exception_Name exceptionN ){ // catch block } |
- Apart from the fact that we have one try statement, we can have many catch statement.
- The ‘ExceptionName’ is the name of the Exception for being caught.
- The exception1, exception2, exception3 and exceptionN are your defined names for referring to the exceptions.
Try/catch Example 1:
#include<iostream> #include<vector> using namespace std; int main(){ vector<int> vec; vec.push_back(0); vec.push_back(1); // access the third element, that doesn't exist try{ vec.at(2); } catch (exception& ex) { cout << "Exception occurred!" << endl; } return 0; } |
Output:
Exception Occurred!
Try/catch Code Explanation:
- Include the ‘iostream’ header file in the program to utilise its functions.
- Include the ‘vector’ header file in the program to utilise its functions.
- Then include the ‘std namespace’ in the program to its classes without calling it.
- Call the main() function. The program logic ought to be added inside its body.
- Create a vector name vec to store integer data.
- Add component 0 to the vector named vec.
- Add component 1 to the vector named vec.
- Utilise the try statement to catch an exception. The { marks the start of the body try/catch block. The code added inside the body will turn into the protected code.
- Attempt to access the element stored in index 2 (third element) of the vector named vec. This component doesn’t exist.
- End of the body of the try/catch block.
- Catch the exception. The returned error message will be put away in the variable ex.
- Print out some message on the comfort if the exception is caught.
- End of the body of the catch block.
- The program should return a value upon fruitful execution.
- End of the main() function body.
How to use try-catch in c++?
Try-catch is an important keyword while performing exceptional conditions.
In the Try block, the “throw” keyword throws an exception when the code detects a problem, which lets us create a custom error.
Now “catch” keyword comes into a picture i.e. “catch” keyword allows you to define a block of code to be executed if an error occurs in the try block.
How do you catch exceptions in C++?
To catch exceptions, a part of the code is kept underinspection. This is done by closing that part of the code in a try-block. When an exceptional circumstance arises within that block, an exception is thrown and an exception handler takes control over the program.