C++ 异常处理例子


#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;
#ifdef _WIN32
    //#include <vld.h>
    #include <Windows.h>
#endif


void test_exit001()
{
    printf("test_exit001\n");
}


void test_exit002()
{
    printf("test_exit002\n");
}

void test_exit003()
{
    printf("test_exit003\n");
}
double mydiv(int a,int b)
{
    if(b==0)
    {
        //throw b;
        throw "b=0";
    }
    return a/b;
}


void test_atexit()
{
    //先注册的最后执行 类似stack。
    atexit(test_exit001);//首先压入栈
    atexit(test_exit002);
    atexit(test_exit003);
    cout<<"test hash"<<endl;
   // exit(0);
    //下面都是异常退出
    exit(-1);
   // terminate();
    //abort();
    cout<<"test hash end"<<endl;
}

template <typename T >  bool ip_check( const char * ip )
{
    if ( true ) //示例
    {
        throw  T(ip) ;
    }
    cout<<ip<<endl;
    return  false;
}
bool ip_check(const char* ip)
{
    cout<<ip<<endl;
    return true ;
}


//http://www.cplusplus.com/reference/stdexcept/
int test_std_exception (void )
{

    try {

        ip_check("192.168.0.1") ;
        ip_check<std::out_of_range>("192.168.0.2") ;
        ip_check<std::logic_error>("192.168.0.3") ;
    } catch (std::bad_alloc)
    {
        cerr << "new run out of memory\n";
    }catch(std::logic_error& e )
    {
        std::cout<<"std::logic_error="<< e.what()  << "\n";
    }catch(std::runtime_error& e )
    {
        std::cout<<"std::runtime_error="<< e.what()  << "\n";
    }catch(std::out_of_range& e )
    {
     std::cout<<"std::out_of_range="<< e.what()  << "\n";
    }catch(std::exception& e )
    {
        std::cout<<"std::exception="<< e.what()  << "\n";
    }

    return 0 ;
}

//自定义异常
class NumberParseException {

public:
    NumberParseException(char* str_input,char* descript_input)
    {
        memset(str,0,sizeof(str));
        memset(descript,0,sizeof(descript));
        if(str_input)
            strcpy(str,str_input);
        if(descript_input)
            strcpy(descript,descript_input);
    };
    ~NumberParseException()
    {
        /*
        if(str)
        {
            delete str;
            str=NULL;
        }
        if(descript)
        {
            delete descript;
            descript=NULL;
        }*/
    }
    void _what() const
    {
        if(str)
        {
            cout<<str<<" ";
        }
        if(descript)
        {
            cout<<descript;
        }
        cout<<endl;
    }

private:
     char str[1024];
     char descript[1024];
};

bool isNumber(char * str)
{
     using namespace std;
     if (str == NULL)
        return false;
     int len = strlen(str);
     if (len == 0)
        return false;
     bool isaNumber = false;
     char ch;
     for (int i = 0; i < len; i++) {
         if (i == 0 && (str[i] == '-' || str[i] == '+'))
            continue;
         if (isdigit(str[i])) {
            isaNumber = true;
         } else {
           isaNumber = false;
           break;
         }
     }
     return isaNumber;
}
//抛出异常
int parseNumber(char * str) throw(NumberParseException)
{
    if (!isNumber(str))
       throw NumberParseException(str,"不是整数");
    return atoi(str);
}

void test_custom_exception()
{

    {
        char *str1 = "1", *str2 = NULL;
        try {
            int num1 = parseNumber(str1);
            int num2 = parseNumber(str2);
            printf("sum is %d\n", num1 + num2);
        } catch (NumberParseException &e) {
            e._what();
        }
    }
    char *str1 = "1", *str2 = "1024da";
    try {
        int num1 = parseNumber(str1);
        int num2 = parseNumber(str2);
        printf("sum is %d\n", num1 + num2);
    } catch (NumberParseException &e) {
        e._what();
    }

}

//http://www.cplusplus.com/reference/stdexcept/
void main()
{
    test_custom_exception();

    test_std_exception();

    try{
        cout<<mydiv(12,6)<<endl;
        cout<<mydiv(15,0)<<endl;
    }catch(char* data)
    {
        cout<<"catch char*="<<data<<endl;
    }catch(int data)
    {
        cout<<"catch int="<<data<<endl;
    }catch(float data)
    {
        cout<<"catch float="<<data<<endl;
    }catch(...)//捕获任意异常
    {
        cout<<"catch all "<<endl;
    }
    test_atexit();



}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值