#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();
}
C++ 异常处理例子
最新推荐文章于 2024-08-04 20:27:25 发布