首页 > 编程语言 >C++ signal(SIGFPE,handler) ignore division by 0 exception

C++ signal(SIGFPE,handler) ignore division by 0 exception

时间:2023-11-15 22:56:58浏览次数:41  
标签:division exception SIGFPE ignore int handler include

#include <stdexcept>
#include <chrono>
#include <csetjmp>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <signal.h>
#include <sstream>
#include <thread>
#include <uuid/uuid.h>
#include <vector>
   
void ignore_exception();
int i=-10;
void my_handler(int para)
{     
    ignore_exception();
}
void ignore_exception()
{
    signal(SIGFPE,my_handler);
    int result=0; 
    while(++i<10)
    {
        try
        {
            result=10/i;
            std::cout << "i:"<<i<<",result:"<<result<<std::endl;
        }
        catch (const std::exception &e)
        {
            std::cout << "In catch!" << std::endl;
            std::cerr << e.what() << '\n';            
        }
    }
}

int main(int args, char **argv)
{
    ignore_exception();
    std::cout << __DATE__ << "_" << __TIME__ << " finished line: " << __LINE__ << " of " << __FUNCTION__ << std::endl;
}

 

g++ -g -std=c++23 -I. main.cpp  -o h1;

 

 

标签:division,exception,SIGFPE,ignore,int,handler,include
From: https://www.cnblogs.com/Fred1987/p/17835055.html

相关文章