C++异常处理机制

extern "C" void mySystemCatchFun();
void sigroutine(int dunno);
int main(int argc, char *argv[])
{
    //cnmps();
    int appReturn ;
    mySystemCatchFun();

    //
    char *tmp = getenv("MPS_HOME");

    QApplication *a = new QApplication(argc, argv);
    char dbPath[256];
    strcpy(dbPath, tmp);
    strcat(dbPath, "/bin/resource/splash.png");
    QPixmap pixmap(dbPath);
    QSplashScreen *pSplash = new QSplashScreen(pixmap);
    pSplash->setParent(0);
    Qt::WindowFlags flags = 0;
    flags =  Qt::Window | Qt::MSWindowsFixedSizeDialogHint | Qt::SplashScreen;
    pSplash->setWindowFlags(flags);
    pSplash->show();
    Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop;
    pSplash->showMessage(QObject::tr("Start MPS Sysetem..."),topRight,Qt::white);
    QTimer::singleShot(1500, a, SLOT(quit()));
    a->exec();
    delete pSplash;
    delete a;
    QApplication::setStyle(QStyleFactory::create("plastique"));
    QApplication::setPalette(QApplication::style()->standardPalette());


    CKsMPS app(argc, argv);
    app.setQuitOnLastWindowClosed(true);
    QObject::connect(&app,SIGNAL(aboutToQuit()),&app,SLOT(quit()));
    //return app.exec();

    appReturn =  app.exec();


    return appReturn;



}
void mySystemCatchFun()
{
    // sighandler_t signal(int signum, sighandler_t handler);
    signal(SIGABRT, sigroutine);
    signal(SIGBUS, sigroutine);
    signal(SIGSEGV, sigroutine);
    signal(SIGFPE, sigroutine);
    signal(SIGILL, sigroutine);
    signal(SIGSTKFLT, sigroutine);
    signal(SIGTRAP, sigroutine);
}

void sigroutine(int dunno)
{
    const int MAX_CALLSTACK_DEPTH = 10;
    void *traceback[MAX_CALLSTACK_DEPTH];
    int depth = backtrace(traceback, MAX_CALLSTACK_DEPTH);
    char**Infor = (char**)backtrace_symbols(traceback,depth);
    QString errorStr;

    errorStr.append(QObject::tr("system error infomation:\n    "));
    /* 信号处理例程,其中dunno将会得到信号的值 */
    switch (dunno)
    {
    case SIGABRT:
        errorStr.append(QObject::trUtf8("由调用abort函数产生,进程非正常退出(Core Abort signal from abort(3))"));
        break;
    case SIGBUS:
        errorStr.append(QObject::trUtf8("某种特定的硬件异常,通常由内存访问引起"));
        break;
    case SIGSEGV:
        errorStr.append(QObject::trUtf8("段冲突,对内存中的无效地址进行读写而引起的(Core Invalid memory reference)"));
        break;
    case SIGFPE:
        errorStr.append(QObject::trUtf8("数学相关的异常,如被0除,浮点溢出,等等(Core Floating point exception)"));
        break;
    case SIGILL:
        errorStr.append(QObject::trUtf8("非法指令异常,通常由一个崩溃的程序或无效的共享内存模块引起的(Core Illegal Instruction)"));
        break;
    case SIGSTKFLT:
        errorStr.append(QObject::trUtf8("Linux专用,数学协处理器的栈异常"));
        break;
    case SIGTRAP:
        errorStr.append(QObject::trUtf8("实现相关的硬件异常。一般是调试异常"));
        break;

    }

    //
    errorStr.append(QObject::tr("\n\n Detail backtrace infomation:\n    "));
    for(int i=0;i<depth;i++)
    {
        errorStr.append(QString::number(i+1)+":    ");
        errorStr.append(Infor[i]);
        errorStr.append("\n    ");
    }
    int result = QMessageBox::critical(0,"Error",errorStr,QMessageBox::Ok);
    if(Infor)
    {
        free(Infor);
        Infor = NULL;
    }
    if(QMessageBox::Ok == result)
        exit(1);

    return;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值