How to Catch Floating Point Errors in C++? Last Updated : 29 Jan, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a part of the code that may throw exceptions is enclosed in try-and-catch blocks to handle them when they arise. We can also use try...catch to catch floating point errors but it involves a bit different approach in comparison to catching standard exceptions. In this article, we will look at how to use try catch to catch floating-point exceptions in C++. Catching Floating Point ErrorsFloating-point errors like division by zero can be caught by the try-catch block in C++. We need to include the division code in the try block and catch any exception in the corresponding catch block. C++ Program to Catch Floating Point Error C++ // C++ program to handle division by zero exception #include <iostream> #include <stdexcept> using namespace std; int main() { try { double result = 1.0 / 0.0; // Attempting division by zero cout << "Result: " << result << endl; } catch (const exception& e) { cerr << "Exception caught: " << e.what() << endl; } return 0; } OutputResult: inf Explanation: Within the try block, a division by zero is tried in the above example. The exception will be caught and handled by the catch block and it returns inf. Although more specialized exception types may be used if necessary, the std::exception type is used to capture common exceptions only. In similar way, we can handle other floating point exceptions such as overflow, underflow, etc. Note: We can use the floating point environment from the standard library <cfenv> to check for floating point exceptions but the function used (std::feenableexcept) is not part of the C++ standard it's a GCC extension and may not be available on all platforms. Comment More infoAdvertise with us Next Article How to Catch Floating Point Errors in C++? R ravi86526iv Follow Improve Article Tags : C++ Programs C++ cpp-exception Exception Handling C++-Exception Handling CPP Examples +2 More Practice Tags : CPP Similar Reads How to Convert Float to int in C++? In C++, the float is a short term for floating point value that is used to define numeric values with decimal points. The floating point numbers can be converted to integers using the process called type casting. In this article, we will learn how to convert a float value to an int in C++. Example: 2 min read How to Catch a Specific Exception in C++? In C++, exceptions are runtime anomalies or abnormal conditions that a program encounters during its execution. The process of handling these exceptions is called exception handling. In this article, we will learn how we can catch specific exceptions in C++. Catch a Specific Exception in C++ In C++, 2 min read How to Catch All Exceptions in C++? In C++, exceptions are objects that indicate you have an error in your program. They are handled by the try-catch block in C++. In this article, we will learn how to catch all the exceptions in C++. Catching All Exceptions in C++To catch all kinds of exceptions in our catch block in C++, we can defi 2 min read How to Throw and Catch Exceptions in C++? In C++, exception handling is a mechanism that allows us to handle runtime errors and exceptions are unusual conditions that occur at runtime. In this article, we will learn how to throw and catch exceptions in C++. Throw and Catch Exceptions in C++In C++ exceptions can be "thrown" when an error occ 2 min read Convert Float to String In C++ In this article, we learn how we can convert float to string in C++ using different methods: Using the to_string()Using stringstreamUsing MacrosUsing lexical_cast from the boost library1. Using to_string() The to_string() method takes a single integer variable or other data type and converts it into 3 min read Like