JNI - Divide by Zero. A comparison between Java and C(1)
来源:互联网 发布:聂卫平 知乎 编辑:程序博客网 时间:2024/06/05 02:29
In Java, we can use try catch to handle DivideByZero exceptions.
public class DivideByZero{public static void main(String[] args){try {int d = 5;d = d/(d-5);}catch (Exception e){e.printStackTrace();}<pre name="code" class="cpp">
result:
wsh@wsh-VirtualBox:~/JNI/JNI_CPP/JavaOnly$ java DivideByZero java.lang.ArithmeticException: / by zeroat DivideByZero.main(DivideByZero.java:5)
But in C++, try catch cannot handle it...
#include <iostream>using namespace std;int main(){try{int d = 5;d = d/(d-5);}catch(int e){cout << "Caught divide by zero exception: "<<e<<endl;}return 0;}
result
wsh@wsh-VirtualBox:~/JNI/JNI_CPP/OnlyCpp$ ./TryCatch Floating point exception (core dumped)
In order to detect DivideByZero exception, we have to manually catch and throw. Obviously, it's useless.
#include <iostream>#include <stdio.h>using namespace std;int main(){try{int d = 5;if (d-5 == 0){throw(23333);}d = d/(d-5);}catch(int e){cout << "Caught divide by zero exception: "<<e<<endl;}return 0;}
result
wsh@wsh-VirtualBox:~/JNI/JNI_CPP/OnlyCpp$ ./TryCatch Caught divide by zero exception: 23333
Why doesn't a divide-by-zero cause an exception in C++?
It was a specific design decision of C++ not to handle divide-by-zero; whereas Java and Ada, for example, take the opposite view. Why? The usual answers -- efficiency and an assumption that C++ will tend to be used with more of an awareness of the hardware.
Stroustrup says, in "The Design and Evolution of C++" (Addison Wesley, 1994), "low-level events, such as arithmetic overflows and divide by zero, are assumed to be handled by a dedicated lower-level mechanism rather than by exceptions. This enables C++ to match the behaviour of other languages when it comes to arithmetic. It also avoids the problems that occur on heavily pipelined architectures where events such as divide by zero are asynchronous."
So you must check your divisors yourself, or discover if your hardware maps divide-by-zero onto some other kind of exception and catch that. It's fairly easy to discover the latter. Just put a try {} catch (...) {} around a divide by zero.
http://www.battersea-locksmith.co.uk/briefings/divByZeroInCpp.html
When the system got a DivideByZero exception, it will throw a SIGFPE. We need to modify the handler to print it out.
8) SIGFPE在发生致命的算术运算错误时发出. 不仅包括浮点运算错误, 还包括溢出及除数为0等其它所有的算术的错误。
#include <iostream>#include <csignal>using namespace std;//We can do more things here.//Can we print the stack trace here?void handler(int a){cout <<"Signal "<<a <<" here!"<<endl;//change SIGFPE to the Default Valuesignal(SIGFPE, SIG_DFL);}int main(){signal(SIGFPE,handler);int d = 5;d = d/(d-5);return 0;}
result
wsh@wsh-VirtualBox:~/JNI/JNI_CPP/OnlyCpp$ ./SystemHandler Signal 8 here!Floating point exception (core dumped)
- JNI - Divide by Zero. A comparison between Java and C(1)
- C warning“comparison between pointer and integer [enabled by default]”
- Editorial Divide by Zero and Codeforces Round #399 (Div. 1+2, combined) (A~F)
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) A(贪心)
- Ordered comparison between pointer and zero ('const char *' and 'int')
- A Technical Comparison between PDF and DJVU
- Parameters Transfer between c++ and java by Using JNI
- IDL divide by zero
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) E
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) F
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined)
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined)
- A Comparison between Focus-Group and In-Depth Interview
- Long Msg: java.lang.ArithmeticException: divide by zero
- 报错:java.lang.ArithmeticException:divide by zero
- comparison between rand() and arc4random()
- Comparison Between NetCDF and HDF5
- A Comparison of C# and Java(转贴)
- 静态内部类的基本信息
- debug调试一
- Extjs4文件上传
- iTerm2
- okHttp 使用HTTPS证书
- JNI - Divide by Zero. A comparison between Java and C(1)
- android 多个activity 共用一套广播BroadcastReceiver
- 关于主备dns工作的一些探讨
- 如何将word转换成pdf格式的文档
- Ubuntu1404 Server 部署 Jenkins
- Android-使用Mockito、Robolectric和RxJava及Retrofit进行单元测试
- 分类算法(4) ---- 逻辑回归(LR)
- spring中管理的类是否单例
- UML常见工具之Powerdesigner