11.C++异常处理

本文详细讲解了异常处理的基础概念,包括异常的抛出、捕获和处理,以及如何使用`try-catch`结构、自定义异常类和标准库异常。通过实例演示了如何在C++中实现不同类型的异常处理,以及如何传递参数和处理不同类型的异常对象。
摘要由CSDN通过智能技术生成
  1. 异常的基本处理

    1. 异常处理机制:暂缓问题处理,不在当前函数中处理,在他调用者中处理

    2. 什么是异常,任何东西都可以认为是异常,错误只是异常的一种

    3. 异常一旦被抛出,不做处理,如果引发异常,会调用默认abort终止程序

    4. 捕获和处理异常

      • throw 抛出异常,(可以理解为返回值,值是任何类型都可以,使我们处理异常一个参照)

      • try(检查,捕获)和catch(处理异常)

    5. 不存在异常的描述

      • throw ()

      • noexcept

    6. #include<iostream>
      using namespace std;
      
      int Division(int a, int b) {
      	if (0==b)      //什么异常
      	{
      		//throw 1; //怎么抛出
      
      		//string str = "123";
      		//throw str;   
      		throw 1.2;
      	}
      	return a / b;
      }
      //catch和if else_if 执行机制是一样的,只能执行一个匹配项
      //删减符 ...  任何类型异常都捕获
      //catch(...)
      
      void  print() throw()
      {
      	cout << "当前函数不存在抛出异常操作" << endl;
      }
      void printData() noexcept
      {
      	cout << "新的描述:不存在抛出异常" << endl;
      	//throw 0;  编译会出错,不能抛出
      }
      
      int main() {
      	try
      	{
      		Division(2, 0);//引发异常不作处理
      		cout << "上面如果出错,就不会执行这行代码" << endl;
      	}
      	catch (int)
      	{
      		cout << "捕获抛出类型为int的异常" << endl;
      		cout << "除数不能为零" << endl;		
      	}
      	catch (string)
      	{
      		cout << "捕获抛出类型为string的异常" << endl;
      		cout << "除数不能为零" << endl;
      	}
      	catch (...)
      	{
      		cout << "捕获任何类型的异常" << endl;
      		cout << "除数不能为零" << endl;
      	}
      	return 0;
      }

  2. 异常处理中的传参

    1. catch(int a) //隐藏一个传参操作

    2. 想要处理抛出字符串的异常处理,注意一下string类型与const char* 类型的区别

    3. 也可以抛出自己类的对象

    4. #include<iostream>
      #include <string>
      using namespace std;
      
      class Error
      {
      public:
      	Error(const char* str = "未知错误") :str(str) {}
      	const char* what()const {
      		return str.c_str();
      	}
      protected:
      	string str;
      private:
      };
      
      int Division(int a, int b) {
      	if (0 == b) 
      	{  
      		//注意string和char*的区别
      		//throw string("除数不能为零");
      		//throw "除数不能为零";
      		throw Error("除数不能为零");
      	}
      	return a / b;
      }
      
      void insertArray(int array[], int& index, int posData, int Length) {
      	if (index >=Length)
      	{
      		throw Error("数组下标溢出");
      	}
      	array[index] = posData;
      	index++;
      }
      
      
      int main() {
      	try
      	{
      		Division(2, 0);
      	}
      	catch (string str){
      		cout << str << endl;
      	}
      	catch (const char* str) {
      		cout << str << endl;
      	}
      	catch (Error str) {
      		cout << str.what() << endl;
      	}
      
      	try
      	{
      		int array[3];
      		int index = 0;
      		for (int i = 0; i < 4; i++)
      		{
      			insertArray(array, index, i, 3);
      		}
      		for (int i=0; i < 3; i++)
      		{
      			cout << array[i] << " ";
      		}
      	}
      	catch (Error str)
      	{
      		cout << str.what() << endl;
      	}
      
      
      	return 0;
      }

  3. 自定义异常处理

    1. 写一个类描述异常

    2. 继承标准库中的类描述异常

    3. #include<iostream>
      #include<string>
      #include <cstring>
      using namespace std;
      class myException:public exception
      {
      public:
      	myException(string str):exception(str.c_str()){}
      protected:
      private:
      };
      void insertArray(int array[], int& index, int posData, int Length) {
      	if (index >= Length)
      	{
      		throw myException("数组下标溢出");
      	}
      	array[index] = posData;
      	index++;
      }
      void deleteArray(int array[], int& index) {
      	if (0 >= index)
      	{
      		throw myException("数组为空,无法删除");
      	}
      	index--;
      }
      int main() {
      	try
      	{
      		int array[3];
      		int index = 0;
      		int length = 3;
      		for (int i = 0; i < 3; i++)
      		{
      			insertArray(array, index, i, length);
      		}
      		for (int i = 0; i < 4; i++)
      		{
      			deleteArray(array, index);
      		}
      		for (int i = 0; i < index; i++)
      		{
      			cout << array[i] << " ";
      		}
      	}
      	catch (myException& object)
      	{
      		cout << object.what() << endl;
      	}
      	return 0;
      }

  4. 标准库中的异常

    #include <exception>
    #include <iostream>
    using namespace std;
    class Exception
    {
    public:
    	Exception(const char* ptr = "UNKNOW") :ptr(const_cast<char*>(ptr)) {}
    	virtual const char* what() const
    	{
    		return ptr;
    	}
    protected:
    	char* ptr;
    };
    class Bad_alloc :public Exception
    {
    public:
    	Bad_alloc(const char* _Message = "bad exception") :Exception(_Message) {}
    protected:
    };
    class Run_time :public Exception
    {
    public:
    	Run_time(const char* _Message = "run_time error") :Exception(_Message) {}
    protected:
    };
    int main()
    {
    	try
    	{
    		while (1)
    		{
    			int* p = new int[1024 * 1024 * 10];
    		}
    	}
    	catch (bad_alloc& object)
    	{
    		cout << object.what() << endl;
    	}
    	return 0;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值