C++类模板中使用异常知识点
来源:互联网 发布:mac废纸篓清空不了 编辑:程序博客网 时间:2024/06/11 08:59
这一篇主要记录了类模板中使用异常类的知识点
类模板中使用异常类的时候,异常类同样可以进行模板化
下面通过这个小案例来明白这个知识点
#include <iostream>#include <cstring>using namespace std;class Error :public exception{};template <class T>class Myerror{public:void showError(){ if(std::strcmp(typeid(T).name(),"int") == 0){ cout << "int error" << endl; }else if(std::strcmp(typeid(T).name(),"char") == 0){ cout << "char error" << endl; }}};template <class T>class PrintTest{public:PrintTest(T t){ if(t < 10){ throw &Myerror<T>(); }else if( t < 'B'){ throw &Myerror<T>(); }}template<class T>class MyPrint{public:MyPrint(T t){ if(t < 20){ throw &Myerror<T>(); }else if(t < 'B'){ throw &Myerror<T>(); }}};};template<class T>void testException(T data){try{PrintTest<T> print2(data);}catch(Myerror<T>* error){error->showError();}}template<class T>void testException2(T data){try{PrintTest<T>::MyPrint<T> mp(data);}catch(Myerror<T>* error){error->showError();}}void main(){testException2<char>('A');cin.get();}
std标准异常的使用:
#include <iostream>#include <stdexcept>using namespace std;class Student{private:int *pstart;public:Student(){pstart = new int[5];}int operator[](int index){ if(index < 0){ throw std::out_of_range("ba la la"); }else if(index < 2){ throw std::logic_error("hahahha"); } return pstart[index];}};void main(){try{Student stu;cout << stu[1] << endl;}catch(exception ex){cout << "error : because of" << ex.what() << endl;} cin.get();}
0 0
- C++类模板中使用异常知识点
- C++:模板,string类,异常
- c++知识点---类模板的使用
- 类中使用模板
- 类模板知识点梳理
- 【C/C++】C++异常基础知识点
- Objective-C中使用@try处理异常
- Objective-C中使用@try处理异常
- Objective-C中使用@try处理异常
- Objective-C中使用@try处理异常
- C#中常见异常类
- c++(1) 类模板/构造函数/捕获异常 实例
- 异常类模板
- 模板类和异常
- c++知识点---函数模板的使用
- 模板中含有类的异常的捕获
- C++中类模板实例
- C++_类模板的使用
- 轮询 设备驱动
- shell脚本调试
- VS2013环境下GSL数学库的使用说明(亲测)
- 饭卡(DP)
- 异步事件模型
- C++类模板中使用异常知识点
- ThreadPoolExecutor线程池参数设置技巧
- 一天搞定CSS:文本text--05
- C++创建单链表
- 蒟蒻养成记——构造数列(2)
- NOJ_1004
- Leetcode学习(17)—— Single Number
- turtle图形函数画图
- 记一次appium识别toast问题解决经历