C++语言基础 例程 异常处理的方法

简介: 贺老师的教学链接  本课讲解例:防止除数为0#include <iostream>using namespace std;template <typename T>T Div(T x,T y){ if(y==0) throw y;//抛出异常 return x/y;}int main(){ int x=5,y

贺老师的教学链接  本课讲解


例:防止除数为0

#include <iostream>
using namespace std;
template <typename T>
T Div(T x,T y)
{
    if(y==0)
        throw y;//抛出异常
    return x/y;
}


int main()
{
    int x=5,y=0;
    double x1=5.5,y1=0.0;
    try
    {
        //被检查的语句
        cout<<x<<"/"<<y<<"="<<Div(x,y)<<endl;
        cout<<x1<<"/"<<y1<<"="<<Div(x1,y1)<<endl;
    }
    catch(int)//异常类型
    {
        cout<<"除数为0,计算错误!"<<endl;//异常处理语句
    }
    catch(double)//异常类型
    {
        cout<<"除数为0.0,计算错误!"<<endl;//异常处理语句
    }
    return 0;
}


异常处理再例:求三角形周长
#include <iostream>
#include <stdexcept>
using namespace std;
int triangle(int a, int b, int c)
{
    if(a<0 || b<0 || c<0 || a+b<=c || a+c<=b || b+c<=a)
        throw runtime_error("The lengths of three sides can't form triangle");
    return a + b + c;
}


int main()
{
    int total = 0;
    try
    {
        total = triangle(3,4,7);
    }
    catch(const runtime_error& e)
    {
        cout<<e.what()<<endl;
    }
    cout<<total<<endl;
    return 0;
}


在异常处理中处理析构函数
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
    Student(int n,string nam):num(n), name(nam)
    {
        cout<<"constructor-"<<n<<endl;
    }
    ~Student( )
    {
        cout<<"destructor-"<<num<<endl;
    }
    void get_data( );
private:
    int num;
    string name;
};
void Student::get_data( )
{
    if(num==0)
        throw num; //如num=0,抛出int型变量num
    else
        cout<<num<<" "<<name<<endl;
    cout<<num<<" is in get_data()!"<<endl;
}


void fun( )
{
    Student stud1(1101,"Tan");
    stud1.get_data( );
    Student stud2(0,"Li");
    stud2.get_data( );
}


int main( )
{
    try
    {
        fun( );
    }
    catch(int n)
    {
        cout<<"num="<<n<<",error!"<<endl;
    }
    return 0;
}


在函数嵌套的情况下检测异常处理
#include <iostream>
using namespace std;
int main( )
{
    void f1( );
    try
    {
        f1( );   //调用f1( )
    }
    catch(double)
    {
        cout<<"OK0!"<<endl;
    }
    cout<<"end0"<<endl;
    return 0;
}
void f1( )
{
    void f2( );
    try
    {
        f2( );   //调用f2( )
    }
    catch(char)
    {
        cout<<"OK1!";
    }
    cout<<"end1"<<endl;
}
void f2( )
{
    void f3( );
    try
    {
        f3( );   //调用f3( )
    }
    catch(int)
    {
        cout<<"Ok2!"<<endl;
    }
    cout<<"end2"<<endl;
}
void f3( )
{
    double a=0;
    try
    {
        throw a;   //抛出double类型异常信息
    }
    catch(float)
    {
        cout<<"OK3!"<<endl;
    }
    cout<<"end3"<<endl;
}


目录
相关文章
|
2天前
|
程序员 编译器 C语言
【C++高阶(七)】C++异常处理的方式
【C++高阶(七)】C++异常处理的方式
|
9天前
|
缓存 编译器 API
NumPy与其他语言(如C/C++)的接口实践
【4月更文挑战第17天】本文介绍了NumPy与C/C++的接口实践,包括Python与C/C++交互基础、NumPy的C API和Cython的使用。通过案例展示了如何将C++函数与NumPy数组结合,强调了内存管理、类型匹配、错误处理和性能优化的最佳实践。掌握这些技能对于跨语言交互和集成至关重要。
|
18天前
|
程序员 C++
C++语言模板学习应用案例
C++模板实现通用代码,以适应多种数据类型。示例展示了一个计算两数之和的模板函数`add&lt;T&gt;`,可处理整数和浮点数。在`main`函数中,展示了对`add`模板的调用,分别计算整数和浮点数的和,输出结果。
12 2
|
28天前
|
C++
11. C++异常处理
11. C++异常处理
16 0
11. C++异常处理
|
29天前
|
存储 算法 数据管理
C++中利用随机策略优化二叉树操作效率的实现方法
C++中利用随机策略优化二叉树操作效率的实现方法
77 1
|
30天前
|
存储 算法 数据库
【C/C++ 数据结构 】树的 四种表示方法
【C/C++ 数据结构 】树的 四种表示方法
30 0
|
1月前
|
Java API 开发工具
【软件设计师备考 专题 】C、C++、Java、Visual Basic、Visual C++等语言的基础知识和应用(三)
【软件设计师备考 专题 】C、C++、Java、Visual Basic、Visual C++等语言的基础知识和应用
30 0
|
1月前
|
Java 数据处理 数据库
【软件设计师备考 专题 】C、C++、Java、Visual Basic、Visual C++等语言的基础知识和应用(二)
【软件设计师备考 专题 】C、C++、Java、Visual Basic、Visual C++等语言的基础知识和应用
34 0
|
1月前
|
存储 算法 Java
【软件设计师备考 专题 】C、C++、Java、Visual Basic、Visual C++等语言的基础知识和应用(一)
【软件设计师备考 专题 】C、C++、Java、Visual Basic、Visual C++等语言的基础知识和应用
34 0
|
1月前
|
算法 Unix Linux
【C/C++ 疑难解决】深入解析C++链接错误:实用的调试技巧和方法
【C/C++ 疑难解决】深入解析C++链接错误:实用的调试技巧和方法
13 1