pastebin - collaborative debugging tool
rovema.kpaste.net RSS

Difference between
modified post 5720b by Anonymous on Mon 14th Feb 2022 11:34
original post 4a7905 by Anonymous on Wed 2nd Feb 2022 10:15
Download diff
Show old version | new version | both versions

    
11
#include <stdlib.h>
22
#include <string.h>
33
#include <stdio.h>
44
#include <setjmp.h>
55
#include <signal.h>
66
#include <math.h>
77
#include <fenv.h>
8+
#include <limits.h>
89
910
#include <stdexcept>
1011
#include <iostream>
12+
#include <cfloat>
1113
1214
1315
static sigjmp_buf jmp_env;
1416
static volatile siginfo_t saved_siginfo;
1517
1618
static void sigfpe_handler(int num, siginfo_t *info, void *ucontext)
1719
{
1820
	(void)num;
1921
	(void)ucontext;
2022
2123
	(void)feclearexcept(FE_ALL_EXCEPT);
2224
	(void)memcpy((void *)&saved_siginfo, info, sizeof(saved_siginfo));
2325
24-
#if 0
26+
#if 1
2527
	siglongjmp(jmp_env, 1);
2628
#else
2729
	throw std::invalid_argument( "you violated the fp math rules" );
2830
#endif
2931
}
3032
3133
int main(int ac, char *av[])
3234
{
3335
	struct sigaction action;
3436
	int ret;
3537
	int i = 0, j = 0;
3638
3739
	(void)puts("#start.");
3840
3941
	(void)memset(&action, 0, sizeof action);
4042
4143
	action.sa_sigaction = sigfpe_handler;
4244
	(void)sigemptyset(&action.sa_mask);
4345
	action.sa_flags = SA_SIGINFO;
4446
	(void)sigaction(SIGFPE, &action, NULL);
4547
48+
        int mask = feenableexcept(
4655
4756
	try
4857
	{
4958
		ret = sigsetjmp(jmp_env, 1);
5059
		if (ret) {
5160
			(void)puts("#got_signal");
5261
		}
5362
		else
5463
		{
5564
			(void)puts("#begin_math");
65+
#if 1
5673
			i = i / j;
74+
#endif
5781
			(void)puts("#end_math");
5882
		}
5983
	}
6084
	catch( const std::invalid_argument &e )
6185
	{
62-
		std::cout << std::string("#got_exception ")+e.what()+" \n";
86+
		std::cout << std::string("#got_exception '") + e.what() + "' \n";
6387
	}
6488
6589
	(void)printf("i=%d, j=%d\n", i, j);
6690
6791
	(void)puts("#end.");
6892
	return EXIT_SUCCESS;
6993
}
7094

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at