2012年(1008)
分类:
2012-08-01 11:20:59
原文地址:异常处理简单应用 作者:luozhiyong131
程序中YC类是表示异常的类,当除数C为零时,由throw(YC)抛出一个YC类,由catch(YC)语句来捕获异常,并处理异常
#include
using namespace std;
int test(int a,int b){
int c;
c = a / b;
return c;
}
int main()
{
int c = test(1,1);
cout<<c<<endl;
cout<<"程序运行到这里!"<<endl;
}
#include
using namespace std;
int test(int a,int b){
int c;
if (b == 0)
{
cout<<"除数为零!"<<endl;
exit(0);
}else{
c = a / b;
}
return c;
}
int main()
{
int c = test(1,0);
cout<<c<<endl;
cout<<"程序运行到这里!"<<endl;
}
#include
using namespace std;
class DivideZero
{
};
int test(int a,int b){
int c;
try
{
if ( b == 0)
throw DivideZero();
c = a / b;
}
catch (DivideZero)
{
cout<<"除数为0"<<endl;
}
return c;
}
int main()
{
int c = test(1,0);
cout<<c<<endl;
cout<<"程序运行到这里!"<<endl;
}
#include
using namespace std;
class DivideZero
{
};
int test(int a,int b){
int c;
if ( b == 0)
throw DivideZero();
c = a / b;
return c;
}
int main()
{
int c ;
try
{
c = test(1,0);
}
catch (DivideZero )
{
cout<<"除数为0"<<endl;
}
cout<<c<<endl;
cout<<"程序运行到这里!"<<endl;
}
#include
using namespace std;
class DivideZero
{
private:
string msg;
public:
DivideZero(string msg){
this->msg = msg;
}
void what(){
cout<<msg<<endl;
}
};
int test(int a,int b) {
int c;
if ( b == 0)
throw DivideZero("除数为0");
c = a / b;
return c;
}
int main()
{
int c ;
try
{
c = test(1,0);
}
catch (DivideZero e)
{
e.what();
}
cout<<c<<endl;
cout<<"程序运行到这里!"<<endl;
}
#include
#include
using namespace std;
class DivideZero
{
private:
string msg;
public:
DivideZero(string msg){
this->msg = msg;
}
string what(){
return msg;
}
};
class MaxExc
{
private:
string msg;
public:
MaxExc(string msg){
this->msg = msg;
}
string what(){
return msg;
}
};
int test(int a,int b) {
int c;
if ( b == 0)
throw DivideZero("除数为0");
if ( a > 100)
throw MaxExc("被除数不能大于100");
c = a / b;
return c;
}
int main()
{
int c ;
try
{
c = test(101,1);
}
catch (DivideZero e)
{
cout<<e.what()<<endl;
}
catch (MaxExc e)
{
cout<<e.what()<<endl;
}
cout<<c<<endl;
cout<<"程序运行到这里!"<<endl;
}
#include
#include
using namespace std;
class BaseExce
{
protected:
string msg;
public:
virtual string what(){
return msg;
};
};
class DivideZero:public BaseExce
{
public:
DivideZero(string msg){
this->msg = msg;
}
string what(){
return msg;
}
};
class MaxExc:public BaseExce
{
public:
MaxExc(string msg){
this->msg = msg;
}
string what(){
return msg;
}
};
class UnknownExec:public BaseExce
{
public:
UnknownExec(string msg){
this->msg = msg;
}
string what(){
return msg;
}
};
int test(int a,int b) {
int c;
if ( b == 0)
throw DivideZero("除数为0");
if ( a > 100)
throw MaxExc("被除数不能大于100");
if ( a == 50)
throw UnknownExec("未知异常");
c = a / b;
return c;
}
int main()
{
int c ;
try
{
c = test(50,1);
}
catch (DivideZero e)
{
cout<<e.what()<<endl;
}
catch (MaxExc e)
{
cout<<e.what()<<endl;
}
catch (BaseExce e)
{
cout<<e.what()<<endl;
}
cout<<c<<endl;
cout<<"程序运行到这里!"<<endl;
}
#include
#include
using namespace std;
class BaseExce
{
protected:
string msg;
public:
virtual string what(){
return msg;
};
};
class DivideZero:public BaseExce
{
public:
DivideZero(string msg){
this->msg = msg;
}
string what(){
return msg;
}
};
class MaxExc:public BaseExce
{
public:
MaxExc(string msg){
this->msg = msg;
}
string what(){
return msg;
}
};
class UnknownExec:public BaseExce
{
public:
UnknownExec(string msg){
this->msg = msg;
}
string what(){
return msg;
}
};
int test(int a,int b) {
int c;
if ( b == 0)
throw exception();
if ( a > 100)
throw exception();
if ( a == 50)
throw exception();
c = a / b;
return c;
}
int main()
{
int c ;
try
{
c = test(50,1);
}
catch (exception e)
{
cout<<e.what()<<endl;
}
cout<<c<<endl;
cout<<"程序运行到这里!"<<endl;
}
#include
#include
using namespace std;
class BaseExce
{
protected:
string msg;
public:
virtual string what(){
return msg;
};
};
class DivideZero:public BaseExce
{
public:
DivideZero(string msg){
this->msg = msg;
}
string what(){
return msg;
}
};
class MaxExc:public BaseExce
{
public:
MaxExc(string msg){
this->msg = msg;
}
string what(){
return msg;
}
};
class UnknownExec:public BaseExce
{
public:
UnknownExec(string msg){
this->msg = msg;
}
string what(){
return msg;
}
};
int test(int a,int b) {
int c;
if ( b == 0)
throw out_of_range("超出边界");
if ( a > 100)
throw exception();
if ( a == 50)
throw exception();
c = a / b;
return c;
}
int main()
{
int c ;
try
{
c = test(50,0);
}
catch (out_of_range e)
{
cout<<e.what()<<endl;
}
cout<<c<<endl;
cout<<"程序运行到这里!"<<endl;
}