![]() |
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 ErrorC++
Output
Result: 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.
|
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |