C++Introduction to C++Language FeatureLearn C++

This Is How To Use Exception Handling In C++ Software

This Is How To Use Exception Handling In C++ Software

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++

we can catch any exceptions by using 3 dots.

we can throw an exception by using throw command, we can catch this exception number as below,

We can throw char as below,

In this example below, e is an exception number and the output will be 99, because we throw exception error 99.

Is there a more complete example of using try and catch in C++?

Here is a full try catch example as below,


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Learn C++Templates

Learn How To Eliminate Pointers In Templates in C++

C++C++11C++14C++17Learn C++Templates

Learn To Export And Import Template Members From A DLL Or A Package In C++

C++C++11C++14C++17Learn C++Templates

What Is A Function Template In C++?

C++C++11C++14C++17Learn C++Templates

What Is A Class Template In C++?