How to Capture the Integer-Divide-By-Zero Error in C++(提前定义信号)
How to Capture the Integer-Divide-By-Zero Error in C++?
MANUAL CAPTURE
The simple and straightforward method is to check it manually if possible. The following outputs “got you!”.
1 |
// https://helloacm.com |
However, it is tedious and not-so-elegant to place checks at each possible code occurrence and we want to the program to handle this.
TRY-CATCH DOES NOT CATCH INT-DIVIDE-BY-ZERO ERROR
The first thought would be to put the possible divide-by-zero code in side a try-catch, like this:
1 |
// https://helloacm.com |
However, when you compile the above code using g++ compiler and run it, the error says Floating point exception instead of the customize error message in try-catch. This means that the error is not caught by try-catch.
USING SIGNAL
At Linux, we can set up signals to capture the error. In this case, the error type is SIGFPE which stands for Signal Float Point Exceptions. The function to set up a signal is signal and it takes two parameters, the signal type and the function to handle.
1 |
// https://helloacm.com |
In short, the when SIGFPE signal is caught, the error function will be invoked to deal with the error. The exit(1) function is important here. In case if you just simply return from the error-handling-function, the disaster happens with an endless loop, which prints “got you!” repeatedly until you ctrl+C to abort/kill it.
When error happens, the OS call the stack of the error function, and set its return address just right before the int-div-by-zero. Virtually, the error-function is invoked right before the error occurs. And after the error-function returns, it will again meet the code that triggers the error, which is why it will keep printing the error.
Some might’ve thought to fix this by making the y variable the global and set to 1 inside the error function. However, this does not help. It seems that the error still occurs regardless the value of y.
1 |
// https://helloacm.com |
USING EXCEPTIONS
We could easily replace above exit() with the exceptions to throw in the error function:
1 |
// https://helloacm.com |
USING CUSTOMIZED SIGNAL
The above throws regardless the type of float point exceptions. The POSIX defines a more comprenhensive signal processing interface, i.e. sigaction, which is defined in csignal header.
1 |
// https://helloacm.com |
Please note that, these two have the same identifies. The first one is the structure type that stores some function pointers. And the second is the actual function entry to set up the signal. The third parameter is usually set to NULL, for more information, read man 3 sigaction.
The sigaction structure has two function entry points:
1 |
void (* sa_handler)(int); |
The first entry is the light-weight handling method, which has been shown in previous code examples. The second one allows more information in the code context. Based on the second parameter, we could pass the additional information via sa_flags. Possible si_code values: FPE_INTOVF (integer overflow), FPE_FLTUND (float underflow), FPE_FLTOVF (float overflow) and what we care about – the FPT_INTDIV.
Let’s look at the complete example of C++ using comprehensive signal to catch the integer-divided-by-zero error.
1 |
// https://helloacm.com |
–EOF (The Ultimate Computing & Technology Blog) —
https://helloacm.com/how-to-capture-the-integer-divide-by-zero-error-in-c/
How to Capture the Integer-Divide-By-Zero Error in C++(提前定义信号)的更多相关文章
- Integer比较
/** * @time 2014-06-25 * @author Cao HaiCheng * */ public class demo { public static void main(Strin ...
- Integer判等的陷阱:你知道Integer内部高速缓冲区IntegerCache吗?
https://blog.csdn.net/magician_Code/article/details/51469101 我们先来看看下面代码的运行情况: public static void mai ...
- IntegerCache缓存占用堆、栈、常量池的问题,自动拆装箱的基本概念,Integer==int时的问题说明
原创声明:作者:Arnold.zhao 博客园地址:https://www.cnblogs.com/zh94 先普及一个基本概念:Java中基本数据类型的装箱和拆箱操作 自动装箱 在JDK5以后,我们 ...
- PHP7函数大全(4553个函数)
转载来自: http://www.infocool.net/kb/PHP/201607/168683.html a 函数 说明 abs 绝对值 acos 反余弦 acosh 反双曲余弦 addcsla ...
- SQL注入备忘单
Find and exploit SQL Injections with free Netsparker SQL Injection Scanner SQL Injection Cheat Sheet ...
- Windows SEH学习 x86
windows 提供的异常处理机制实际上只是一个简单的框架.我们通常所用的异常处理(比如 C++ 的 throw.try.catch)都是编译器在系统提供的异常处理机制上进行加工了的增强版本.这里先抛 ...
- 7系列FPGA的时钟资源——UG472
时钟架构总览 7系的FPGA使用了专用的全局(Global)和区域(Regional)IO和时钟资源来管理设计中各种的时钟需求.Clock Management Tiles(CMT)提供了时钟合成(C ...
- IBM DB2 SQL error code list
SQL return codes that are preceded by a minus sign (-) indicate that the SQL statement execution was ...
- leetcode 学习心得 (1) (24~300)
源代码地址:https://github.com/hopebo/hopelee 语言:C++ 24.Swap Nodes in Pairs Given a linked list, swap ever ...
随机推荐
- Watchdog问题实例分析
1.日志获取 Watchdog相关的问题甚至需要以下所有的日志: logcat 通过adb logcat命令输出Android的一些当前运行日志,可以通过logcat的 -b 参数指定要输出的日志缓冲 ...
- 华为云软件开发云VS开发痛点=?
在软件开发的过程中,是不是总会遇到这些问题: 搭建一个开发环境,用了九牛二虎之力,悲催的是竟然用不了…… 团队同事突然出差,他写的代码出现问题,我却不会改…… 提升软件件质量靠代码,交叉看.找大拿,简 ...
- PL/SQL Developer跑在Oracle 64位数据库上初始化错误
安装完Oracle(64位).PL/SQL Developer后执行PL/SQL出现例如以下的错误: 网上查资料说,我的PL/SQL Developer与ORACLE不兼容,即PL/SQL不支持64位 ...
- scipy 图像处理(scipy.misc、scipy.ndimage)、matplotlib 图像处理
from scipy.misc import imread / imsave / imshow imresize / imrotate / imfilter 1. scipy.misc 下的图像处理 ...
- 聊聊PROFINET与PROFIBUS
1.PROFINET与PROFIBUS从狭义上比,没有可比性,因为他们的物理接口不同,电气特性,不同,波特率不同,电气介质特性不同等等.这样两者的协议是完全没有关联性的,唯一的关联性就是两者都是PI组 ...
- this prototype constructor
http://www.cnblogs.com/phpmix/articles/1734031.html
- python 快速排序 完整
两头开始 以第一个为基准,从有往左,找第一个比基准数 大的,然后交换 从左往右,找第一个比基准数晓得,然后交换 遍历剩下的 基准数 左边的数们 以及 基准数 右边的数们 def quick_so ...
- C#代码实现矢量画图
原文:C#代码实现矢量画图 版权声明:本文为博主原创文章,转载请附上链接地址. https://blog.csdn.net/ld15102891672/article/details/80275969 ...
- Why I Choose Delphi Summary
Over the summer, there has been a number of blog posts on this topic, but I haven't seen a complete ...
- CefSharp中ChromiumWebBrowser打开新页面处理(_blank)
原文:CefSharp中ChromiumWebBrowser打开新页面处理(_blank) 版权声明:欢迎转载,但是请保留出处说明 https://blog.csdn.net/lanwilliam/a ...