JNI - Divide by Zero. A comparison between Java and C(1)

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 zero
	at 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

The reason is that DivideByZero is not regarded as an exception in C++. Look at the following citation:

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.

What is SIGFPE? 
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 Value	
	signal(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)


In this article, we discussed the different exception handling mechanism in Java and C. Next we'll discuss how to make use of the two mechanisms in JNI and Android.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值