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
| 1 | 1 | #include <stdlib.h> | |
| 2 | 2 | #include <string.h> | |
| 3 | 3 | #include <stdio.h> | |
| 4 | 4 | #include <setjmp.h> | |
| 5 | 5 | #include <signal.h> | |
| 6 | 6 | #include <math.h> | |
| 7 | 7 | #include <fenv.h> | |
| 8 | + | #include <limits.h> | |
| 8 | 9 | ||
| 9 | 10 | #include <stdexcept> | |
| 10 | 11 | #include <iostream> | |
| 12 | + | #include <cfloat> | |
| 11 | 13 | ||
| 12 | 14 | ||
| 13 | 15 | static sigjmp_buf jmp_env; | |
| 14 | 16 | static volatile siginfo_t saved_siginfo; | |
| 15 | 17 | ||
| 16 | 18 | static void sigfpe_handler(int num, siginfo_t *info, void *ucontext) | |
| 17 | 19 | { | |
| 18 | 20 | (void)num; | |
| 19 | 21 | (void)ucontext; | |
| 20 | 22 | ||
| 21 | 23 | (void)feclearexcept(FE_ALL_EXCEPT); | |
| 22 | 24 | (void)memcpy((void *)&saved_siginfo, info, sizeof(saved_siginfo)); | |
| 23 | 25 | ||
| 24 | - | #if 0 | |
| 26 | + | #if 1 | |
| 25 | 27 | siglongjmp(jmp_env, 1); | |
| 26 | 28 | #else | |
| 27 | 29 | throw std::invalid_argument( "you violated the fp math rules" ); | |
| 28 | 30 | #endif | |
| 29 | 31 | } | |
| 30 | 32 | ||
| 31 | 33 | int main(int ac, char *av[]) | |
| 32 | 34 | { | |
| 33 | 35 | struct sigaction action; | |
| 34 | 36 | int ret; | |
| 35 | 37 | int i = 0, j = 0; | |
| 36 | 38 | ||
| 37 | 39 | (void)puts("#start."); | |
| 38 | 40 | ||
| 39 | 41 | (void)memset(&action, 0, sizeof action); | |
| 40 | 42 | ||
| 41 | 43 | action.sa_sigaction = sigfpe_handler; | |
| 42 | 44 | (void)sigemptyset(&action.sa_mask); | |
| 43 | 45 | action.sa_flags = SA_SIGINFO; | |
| 44 | 46 | (void)sigaction(SIGFPE, &action, NULL); | |
| 45 | 47 | ||
| 48 | + | int mask = feenableexcept( | |
| 46 | 55 | ||
| 47 | 56 | try | |
| 48 | 57 | { | |
| 49 | 58 | ret = sigsetjmp(jmp_env, 1); | |
| 50 | 59 | if (ret) { | |
| 51 | 60 | (void)puts("#got_signal"); | |
| 52 | 61 | } | |
| 53 | 62 | else | |
| 54 | 63 | { | |
| 55 | 64 | (void)puts("#begin_math"); | |
| 65 | + | #if 1 | |
| 56 | 73 | i = i / j; | |
| 74 | + | #endif | |
| 57 | 81 | (void)puts("#end_math"); | |
| 58 | 82 | } | |
| 59 | 83 | } | |
| 60 | 84 | catch( const std::invalid_argument &e ) | |
| 61 | 85 | { | |
| 62 | - | std::cout << std::string("#got_exception ")+e.what()+" \n"; | |
| 86 | + | std::cout << std::string("#got_exception '") + e.what() + "' \n"; | |
| 63 | 87 | } | |
| 64 | 88 | ||
| 65 | 89 | (void)printf("i=%d, j=%d\n", i, j); | |
| 66 | 90 | ||
| 67 | 91 | (void)puts("#end."); | |
| 68 | 92 | return EXIT_SUCCESS; | |
| 69 | 93 | } | |
| 70 | 94 |
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.
rovema.kpaste.net RSS