Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7651344
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: C/C++

2011-03-31 11:42:06

  1. /****************************************************************************************
  2. * 文件名:
  3. * 功能:异常处理简单应用
  4. * 说明:异常处理机制是C++的一个特色。
  5.         通过三个关键字实现:throw try catch。
  6.         异常处理分为异常的识别与发出、异常捕捉与处理。
  7.         异常处理机制的基本处理方法是:当一个函数发生异常时,便抛出一个异常信息,
  8.         首先由它的调用者捕获并处理,如果调用者不能处理,便报告(抛出)给一级调用者
  9.         直到运行系统,若仍不能处理 ,将简单终止程序。        
  10. * 时间:2011-3-31                                                   --Lzy
  11. *****************************************************************************************/
  12. /*如果在try{}程序块内发现异常,则由throw语句抛出异常,catch语句捕获异常*/
  13. #include <iostream.h>

  14. class YC{};

  15. void main()
  16. {
  17.     double a, b, c;
  18.     cout<<endl<<"输入a,b和c的值:";
  19.     cin>>a>>b>>c;
  20.     
  21.     try
  22.     {
  23.         if (c == 0)
  24.             throw YC();            //抛出异常

  25.         cout<<endl<<a<<"+"<<b<<"/"<<c<<"="<<a+b/c<<endl;
  26.     }

  27.     catch(YC)                    //捕获异常

  28.     {
  29.         cout<<endl<<"除数不能为0!"<<endl;
  30.     }
  31. }

程序中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;

}

 

阅读(1970) | 评论(0) | 转发(2) |
0

上一篇:模板简单应用

下一篇:文件处理简单应用

给主人留下些什么吧!~~