标签:[1] lib span 类型 lag return using 匿名 定义
#include <iostream> #include <string> using namespace std; int main(){ string str = "http://c.biancheng.net"; char ch1 = str[100]; //下标越界,ch1为垃圾值 cout<<ch1<<endl; char ch2 = str.at(100); //下标越界,抛出异常 cout<<ch2<<endl; return 0; }
try{ // 可能抛出异常的语句 }catch(exceptionType variable){ // 处理异常的语句 }
头文件(#include <exception>)
try检测语句是否有异常,若有则运行catch
只要try检测到异常,便会跳到catch执行,不会再来执行检测到异常语句后的语句(try中)
throw可以用来抛出一个异常,让try检测到。
1 #include <iostream> 2 #include <string> 3 #include <exception> 4 using namespace std; 5 6 void func(){ 7 throw "Unknown Exception"; //抛出异常 8 cout<<"[1]This statement will not be executed."<<endl; 9 } 10 11 int main(){ 12 try{ 13 func(); 14 cout<<"[2]This statement will not be executed."<<endl; 15 }catch(const char* &e){ 16 cout<<e<<endl; 17 } 18 19 return 0; 20 }
运行结果:Unknown Exception
1.
catch在匹配过程中的类型转换:
#include <iostream> #include <exception> using namespace std; int main() { char a[5] = "hell"; try{ throw a; } catch(int){ cout << "1" << endl;} catch(const char *){ cout << " 0 "<< endl; } return 0; }
char str[] = "www.baidu.com"; char *pt = str; class Base{}; Base s1; throw 100;//int类型 throw str;//数组类型 throw pt;//指针类型 throw s1;//对象类型
1 #include <iostream> 2 #include <cstdlib> 3 using namespace std; 4 5 //自定义的异常类型 6 class OutOfRange{ 7 public: 8 OutOfRange(): m_flag(1){ }; 9 OutOfRange(int len, int index): m_len(len), m_index(index), m_flag(2){ } 10 public: 11 void what() const; //获取具体的错误信息 12 private: 13 int m_flag; //不同的flag表示不同的错误 14 int m_len; //当前数组的长度 15 int m_index; //当前使用的数组下标 16 }; 17 18 void OutOfRange::what() const { 19 if(m_flag == 1){ 20 cout<<"Error: empty array, no elements to pop."<<endl; 21 }else if(m_flag == 2){ 22 cout<<"Error: out of range( array length "<<m_len<<", access index "<<m_index<<" )"<<endl; 23 }else{ 24 cout<<"Unknown exception."<<endl; 25 } 26 } 27 28 //实现动态数组 29 class Array{ 30 public: 31 Array(); 32 ~Array(){ free(m_p); }; 33 public: 34 int operator[](int i) const; //获取数组元素 35 int push(int ele); //在末尾插入数组元素 36 int pop(); //在末尾删除数组元素 37 int length() const{ return m_len; }; //获取数组长度 38 private: 39 int m_len; //数组长度 40 int m_capacity; //当前的内存能容纳多少个元素 41 int *m_p; //内存指针 42 private: 43 static const int m_stepSize = 50; //每次扩容的步长 44 }; 45 46 Array::Array(){ 47 m_p = (int*)malloc( sizeof(int) * m_stepSize ); 48 m_capacity = m_stepSize; 49 m_len = 0; 50 } 51 int Array::operator[](int index) const { 52 if( index<0 || index>=m_len ){ //判断是否越界 53 throw OutOfRange(m_len, index); //抛出异常(创建一个匿名对象) 54 } 55 56 return *(m_p + index); 57 } 58 int Array::push(int ele){ 59 if(m_len >= m_capacity){ //如果容量不足就扩容 60 m_capacity += m_stepSize; 61 m_p = (int*)realloc( m_p, sizeof(int) * m_capacity ); //扩容 62 } 63 64 *(m_p + m_len) = ele; 65 m_len++; 66 return m_len-1; 67 } 68 int Array::pop(){ 69 if(m_len == 0){ 70 throw OutOfRange(); //抛出异常(创建一个匿名对象) 71 } 72 73 m_len--; 74 return *(m_p + m_len); 75 } 76 77 //打印数组元素 78 void printArray(Array &arr){ 79 int len = arr.length(); 80 81 //判断数组是否为空 82 if(len == 0){ 83 cout<<"Empty array! No elements to print."<<endl; 84 return; 85 } 86 87 for(int i=0; i<len; i++){ 88 if(i == len-1){ 89 cout<<arr[i]<<endl; 90 }else{ 91 cout<<arr[i]<<", "; 92 } 93 } 94 } 95 96 int main(){ 97 Array nums; 98 //向数组中添加十个元素 99 for(int i=0; i<10; i++){ 100 nums.push(i); 101 } 102 printArray(nums); 103 104 //尝试访问第20个元素 105 try{ 106 cout<<nums[20]<<endl; 107 }catch(OutOfRange &e){ 108 e.what(); 109 } 110 111 //尝试弹出20个元素 112 try{ 113 for(int i=0; i<20; i++){ 114 nums.pop(); 115 } 116 }catch(OutOfRange &e){ 117 e.what(); 118 } 119 120 printArray(nums); 121 122 return 0; 123 }
可以在函数头与函数体之间使用throw
double func (char param) throw (int);
声明了一个名为func的函数,返回值类型为double,有一个char类型的参数,并且只能返回一个int类型的异常(若要抛出多个类型异常,则用逗号隔开。如果不抛出
异常,括号内什么都不写)
class Base{ public: virtual int fun1(int) throw(); virtual int fun2(int) throw(int); virtual string fun3() throw(int, string); }; class Derived:public Base{ public: int fun1(int) throw(int); //错!异常规范不如 throw() 严格 int fun2(int) throw(int); //对!有相同的异常规范 string fun3() throw(string); //对!异常规范比 throw(int,string) 更严格 }
//错!定义中有异常规范,声明中没有 void func1(); void func1() throw(int) { } //错!定义和声明中的异常规范不一致 void func2() throw(int); void func2() throw(int, bool) { } //对!定义和声明中的异常规范严格一致 void func3() throw(float, char*); void func3() throw(float, char*) { }
异常规范是 C++98 新增的一项功能,但是后来的 C++11 已经将它抛弃了,不再建议使用。
标签:[1] lib span 类型 lag return using 匿名 定义
原文地址:https://www.cnblogs.com/Mayfly-nymph/p/8965689.html