IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 2.C++异常处理 -> 正文阅读

[C++知识库]2.C++异常处理

2.C++异常处理学习

1.catch捕获异常时,不会进行数据类型的默认转换

1.1 能够正常捕获异常

#include<iostream>
using namespace std;
int main(){
    cout<<"1--befroe try block..."<<endl;
    try{
        cout<<"2--Inside try block..."<<endl;
        throw 10;
        cout<<"3--After throw ...."<<endl;
    }
    catch(int i) {
	    cout<<"4--In catch block1 ... exception..errcode  is.."<<i<<endl;   //捕获异常
    }
    catch(char * s) {
        cout<<"5--In catch block2 ... exception..errcode is.."<<s<<endl;
    }
    cout<<"6--After Catch...";
    system("pause");
}

1--befroe try block...
2--Inside try block...
4--In catch block1 ... exception..errcode  is..10
6--After Catch...

1.2 不能进行隐式转换捕获异常

#include<iostream>
using namespace std;
int main(){
    cout<<"1--befroe try block..."<<endl;
    try{
        cout<<"2--Inside try block..."<<endl;
        throw 10;                        
        cout<<"3--After throw ...."<<endl;
    }
    catch(double i) { 		//不能捕获int类型的异常
        cout<<"4--In catch block1 .. an int type is.."<<i<<endl;
    }
    cout<<"5--After Catch...";
    system("pause");
    return 0;
}
1--befroe try block...
2--Inside try block...
terminate called after throwing an instance of 'int'    

2.try块中一旦抛出异常则不再执行下方代码

#include<iostream>
using namespace std;
void temperature(int t)
{
    
    if(t==100) throw "the temperature is so hot";
    else if(t==0) throw "the temperature is so cool";
    else{cout<<"temperatore="<<t<<endl;}
   
}
int main(){
    try{
        temperature(0);						//L1
        temperature(10);						//L2
        temperature(100);						//L3
    }
    catch(char const*s){cout<<s<<endl;}
    system("pause");
    return 0;
}

the temperature is so cool

3.限制异常的方法

  • 当一个函数声明中不带任何异常描述时,它可以抛出任何异常。
int f(int,char);                 //函数f可以抛出任何异常
  • 在函数声明的后面添加一个throw参数表,在其中指定函数可以抛出的异常类型。
int g(int,char)  throw(int,char);  //只允许抛出int和char异常。
  • 指定throw限制表为不包括任何类型的空表,不允许函数抛出任何异常。
int h(int,char) throw();//不允许抛出任何异常
#include<iostream>
using namespace std;
//内部再次throw异常的时候,函数不要带throw()

void Errhandler(int n)     //可以抛出任何异常
{
    try{
        if(n==1) throw n;
        cout<<"all is ok..."<<endl;
    }
    catch(int n){
        cout<<"catch an int exception inside..."<<n<<endl;
        throw n;                                     //再次抛出本catch捕获的异常
    }
}
int main(){
    try{
        Errhandler(1); 
    }
    catch(int x){ cout<<"catch int an exception in main..."<<x<<endl; }
    cout<<"....End..."<<endl;
    system("pause");
    return 0;
}

catch an int exception inside...1
catch int an exception in main...1
....End...

4.catch捕获异常类型

catch(Exception e); //只能捕获Exception类型的异常

catch(...); //可捕获任意类型的异常
#include<iostream>
using namespace std;
void Errhandler(int n)throw(){
    try{
        if(n==1) throw n;
        if(n==2) throw "dx";
        if(n==3) throw 1.1;
    }
    catch(...){cout<<"catch an exception..."<<endl;}   //可捕获任意类型的异常
}
int main(){
    Errhandler(1);
    Errhandler(2);
    Errhandler(3);
    system("pause");
    return 0;
}

catch an exception...
catch an exception...
catch an exception...

5.catch中可继续抛出异常,等待外部处理

#include<iostream>
using namespace std;
//内部再次throw异常的时候,函数不要带throw()

void Errhandler(int n)     //可以抛出任何异常
{
    try{
        if(n==1) throw n;
        cout<<"all is ok..."<<endl;
    }
    catch(int n){
        cout<<"catch an int exception inside..."<<n<<endl;
        throw n;                                     //再次抛出本catch捕获的异常
    }
}
int main(){
    try{
        Errhandler(1); 
    }
    catch(int x){ cout<<"catch int an exception in main..."<<x<<endl; }
    cout<<"....End..."<<endl;
    system("pause");
    return 0;
}

catch an int exception inside...1
catch int an exception in main...1
....End...

再次抛出异常,如果是catch块无法处理捕获的异常,它可以将该异常再次抛出,使异常能够在恰当的地方被处理。再次抛出的异常不会再被同一个catch块所捕获,它将被传递给外部的catch块处理。要在catch块中再次抛出同一异常,只需在该catch块中添加不带任何参数的throw语句即可。

6.异常类

6.1可自定义异常类

#include <iostream>
using namespace std;
const int MAX=3;
class Full{};	//L1  堆栈满时抛出的异常类
class Empty{};	//L2  堆栈空时抛出的异常类
class Stack{
    private:
        int s[MAX];
        int top;
    public:
        void push(int a);
        int  pop();
        Stack(){top=-1;}
};
void Stack::push(int a){
    if(top>=MAX-1) 
        throw Full();			
    s[++top]=a;
}
int Stack::pop(){
    if(top<0)   throw Empty();	
    return s[top--];
}
int main(){
    Stack s;
    try{
        s.push(10);        s.push(20);        s.push(30);
        //s.push(40);	//L5  将产生栈满异常
        cout<<"stack(0)= "<<s.pop()<<endl;
        cout<<"stack(1)= "<<s.pop()<<endl;
        cout<<"stack(2)= "<<s.pop()<<endl;
        cout<<"stack(3)= "<<s.pop()<<endl;		//L6
    }
    catch(Full){    cout<<"Exception: Stack Full"<<endl;    }
    catch(Empty){	   cout<<"Exception: Stack Empty"<<endl; }
    system("pause");
    return 0;
}

stack(0)= 30
stack(1)= 20
stack(2)= 10
stack(3)= Exception: Stack Empty

6.2异常类的捕获

派生异常类无法捕获基类异常,基类异常类可捕获派生类异常

#include<iostream>
using namespace std;
class BasicException{
    public:
        string Where(){return "BasicException...";}
};
class FileSysException:public BasicException{
    public:
        string Where(){return "FileSysException...";}
};
class FileNotFound:public FileSysException{
    public:
        string Where(){return "FileNotFound...";}
};
class DiskNotFound:public FileSysException{
    public:
        string Where(){return "DiskNotFound...";}
};
int main(){
    try{
//       //派生异常类无法捕获基类异常
         throw FileSysException();
    }
    catch(DiskNotFound p){cout<<p.Where()<<endl;}
    catch(FileNotFound p){cout<<p.Where()<<endl;}
    catch(FileSysException p){cout<<p.Where()<<endl;}
    catch(BasicException p){cout<<p.Where()<<endl;}
    try{
//        .....  //基类异常类可捕获派生类异常
         throw DiskNotFound();
    }
    catch(BasicException p){cout<<p.Where()<<endl;}
    catch(FileSysException p){cout<<p.Where()<<endl;}
    catch(DiskNotFound p){cout<<p.Where()<<endl;}
    catch(FileNotFound p){cout<<p.Where()<<endl;}
}

FileSysException...
BasicException...

6.3异常类多态

#include <iostream>
using namespace std;
class BasicException
{
public:
    virtual string Where() { return "BasicException..."; }
};
class FileSysException : public BasicException
{
public:
    virtual string Where() { return "FileSysException..."; }
};
class FileNotFound : public FileSysException
{
public:
    virtual string Where() { return "FileNotFound..."; }
};
class DiskNotFound : public FileSysException
{
public:
    virtual string Where() { return "DiskNotFound..."; }
};
int main()
{
    try
    {
        DiskNotFound err;
        throw &err;
    }
    catch (BasicException *p)           //可捕获派生类异常
    {
        cout << p->Where() << endl;     //多态调用异常类方法
    }
}

DiskNotFound...

6.4调用异常类成员函数

#include <iostream>
using namespace std;
const int MAX=3;
class Full{
    int a;
    public:
        Full(int i):a(i){}
        int getValue(){return a;}
};
class Empty{};
class Stack{
    private:
        int s[MAX];
        int top;
    public:
        Stack(){top=-1;}
        void push(int a){
            if(top>=MAX-1)  
                throw Full(a);			
            s[++top]=a;
        }
        int pop(){
            if(top<0)
                throw Empty();
            return s[top--];
        }
};
int main(){
    Stack s;
    try{
        s.push(10);  
        s.push(20);
        s.push(30);  
        s.push(40);
    }
    catch(Full e){	
        cout<<"Exception: Stack Full..."<<endl;
        cout<<"The value not push in stack:"<<e.getValue()<<endl; 
    }
    system("pause");
}

Exception: Stack Full...
The value not push in stack:40
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-13 11:45:37  更:2021-08-13 11:48:31 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 12:29:08-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码