/*++ test.cpp
version:1.0
decript:define a exception class named myException
derived from base class exception
which is declared in <exception>
created:2011-08-14
author: btwsmile
--*/
#include<exception>
#include<iostream>
using namespace std;
//customized exception class 'myException'
class myException:public exception
{
public:
myException():exception("ERROR! Don't divide a number by integer zero.\n")
{
}
};
//entry of the application
int main()
{
int x=100,y=0;
try
{
if(y==0) throw myException();
else cout<<x/y;
}
catch(myException& me)
{
cout<<me.what();
}
system("pause");
return 0;
}
ERROR! Don't divide a number by integer zero.
/*++ test.cpp
version:1.1
decript:define a exception class named myException
according to C++ standard,
derived from base class exception
which is declared in <exception>
!also,encapusulate throw into a function
created:2011-08-14
author: btwsmile
--*/
#include<exception>
#include<iostream>
using namespace std;
//customized exception class 'myException'
class myException:public exception
{
public:
const char* what()const throw()//#1
{
return "ERROR! Don't divide a number by integer zero.\n";
}
};
void check(int y) throw(myException)//#2
{
if(y==0) throw myException();
}
//entry of the application
int main()
{
int x=100,y=0;
try
{
check(y);
cout<<x/y;
}
catch(myException& me)
{
cout<<me.what();
}
system("pause");
return 0;
}
/*++ test.cpp
version:1.3
decript:define an unexpected excption handler,
set it by using set_unexpected,
modify the throw list of function check
created:2011-08-14
author: btwsmile
--*/
#include<exception>
#include<iostream>
using namespace std;
//customized exception class 'myException'
class myException:public exception
{
public:
const char* what()const throw()
{
return "ERROR! Don't divide a number by integer zero.\n";
}
};
void check(int y) throw()//#1 only int-type exception is permitted
{
if(y==0) throw myException();
}
void myUnexpected()
{
cout<<"Unexpected exception caught!\n";
system("pause");
exit(-1);
}
//entry of the application
int main()
{
unexpected_handler oldHandler=set_unexpected(myUnexpected);
int x=100,y=0;
try
{
check(y);
cout<<x/y;
}
catch(myException& me)
{
cout<<me.what();
}
system("pause");
return 0;
}
Unexpected exception caught!
/*++ test.cpp
version:1.4.1
decript:
how to understand "exception not caucht"?
created:2011-08-14
author: btwsmile
--*/
#include<exception>
#include<iostream>
using namespace std;
//customized exception class 'myException'
class myException:public exception
{
public:
const char* what()const throw()
{
return "ERROR! Don't divide a number by integer zero.\n";
}
};
void check(int y) //any type of exception is permitted
{
if(y==0) throw myException();
}
void myUnexpected()
{
cout<<"Unexpected exception caught!\n";
system("pause");
exit(-1);
}
//entry of the application
int main()
{
unexpected_handler oldHandler=set_unexpected(myUnexpected);
int x=100,y=0;
try
{
check(y);
cout<<x/y;
}
catch(int &e) //##1 no catch sentence matches the throw type
{
cout<<e<<endl;
}
/* ##2 if add this part, any type which's not handler before will
be caught
catch(...)
{
cout<<"Unkown exception caught!\n";
}
*/
system("pause");
return 0;
}
This application has requested the Runtime to terminate it in an unusual way.Please contact the application's support team for more information.
/*++ test.cpp
version:1.4.2
decript:
how to understand "exception not caucht"?
created:2011-08-14
author: btwsmile
--*/
#include<exception>
#include<iostream>
using namespace std;
//customized exception class 'myException'
class myException:public exception
{
public:
const char* what()const throw()
{
return "ERROR! Don't divide a number by integer zero.\n";
}
};
void check(int y) //any type of exception is permitted
{
if(y==0) throw myException();
}
void myUnexpected()
{
cout<<"Unexpected exception caught!\n";
system("pause");
exit(-1);
}
void myTerminate() //##1 set it be the terminate handler
{
cout<<"Unhandler exception!\n";
system("pause");
exit(-1);
}
//entry of the application
int main()
{
unexpected_handler oldHandler=set_unexpected(myUnexpected);
terminate_handler preHandler=set_terminate(myTerminate);
int x=100,y=0;
try
{
check(y);
cout<<x/y;
}
catch(int &e) //no catch sentence matches the throw type
{
cout<<e<<endl;
}
system("pause");
return 0;
}
Unhandler exception!
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有