Do you want to use exception handling to trap runtime errors in your C++ software? How can we use try
and catch
in C++? Is there a C++ example for the latest try and catch usage? What is difference between try
and __try
? Can we use __try
in C++?
C and C++ has great features when it comes to Exception Handling. Exception Handling handles errors or exceptions which occur for code lines listed inside the try { } statement. It finds run-time anomalies or abnormal conditions that a program encounters during its execution, in other words when the user is actually using it. There are Synchronous and Asynchronous exceptions methods. The try keyword is supported only in C++ programs. Use __try in C programs. C++ also allows __try.
How do I create an exception handler in C++?
A block of code in which an exception can occur must be prefixed by the keyword try. Following the try keyword is a block of code enclosed by braces. This indicates that the program is prepared to test for the existence of exceptions. If an exception occurs, the program flow is interrupted. The sequence of steps taken is as follows:
- The program searches for a matching handler
- If a handler is found, the stack is unwound to that point
- Program control is tranferred to the handler
If no handler is found, the program will call the terminate function. If no exceptions are thrown, the program executes in the normal fashion. RAD Studio, C++ Builder 10.4.2 has an updated Exceptions Handling for the Clang-enhanced compiler. This update has reworking exception handling in the RTL for Clang-enhanced compilers and STL, an updated version of Dinkumware with many tweaks and fixes.
C++ Builder provides following keywords to handle exceptions.
try: try a statement block that throw an exception.
catch(): is a statement block that is executed when a particular exception is thrown.
throw(): Throws an exception number or character. It is also used to list the exceptions that a function throws, but it doesn’t handle itself.
Is there a simple example of using C++ try and catch to handle exceptions?
Here is a simple try and catch example in C++
1 2 3 4 5 6 7 |
try { // your codes here } catch (int e) { } |
we can catch any exceptions by using 3 dots.
1 2 3 4 5 6 7 |
try { // your codes here } catch(...) // catch any exception { } |
we can throw an exception by using throw command, we can catch this exception number as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> int main() { try { throw 99; } catch (int e) { std::cout << "An exception occurred. Exception Nr. " << e << '\n'; } getchar(); return 0; } |
We can throw char as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> int main() { try { throw 'X'; } catch (char e) { std::cout << "An exception occurred. Exception Nr. " << e << '\n'; } getchar(); return 0; } |
In this example below, e is an exception number and the output will be 99, because we throw exception error 99.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <exception> int main() { try { // codes to be checked } catch (int e) { std::cout << "int exception"; } catch (char e) { std::cout << "char exception"; } catch (...) { std::cout << "default exception"; } } |
Is there a more complete example of using try and catch in C++?
Here is a full try catch example as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
int main() { float e, f, g; try { try { f = 1.0; g = 0.0; try { puts("Another exception:"); e = f / g; } __except(EXCEPTION_EXECUTE_HANDLER) { puts("Caught a C-based exception."); throw(Exception("Hardware error: Divide by 0")); } } catch(const Exception& e) { printf("Caught C++ Exception: %s :\n", e.msg()); } } __finally { puts("C++ allows __finally too!"); } getchar(); return e; } |
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition