catch抛出异常:
为什么要在catch中抛出异常呢?
异常的重新解释实验:
1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 6 void Demo() 7 { 8 try 9 { 10 try 11 { 12 throw ‘c‘; 13 } 14 catch(int i) 15 { 16 cout << "Inner: catch(int i)" << endl; 17 throw i; 18 } 19 catch(...) 20 { 21 cout << "Inner: catch(...)" << endl; 22 throw; 23 } 24 } 25 catch(...) 26 { 27 cout << "Outer: catch(...)" << endl; 28 } 29 } 30 31 32 /* 33 假设: 当前的函数式第三方库中的函数,因此,我们无法修改源代码 34 35 函数名: void func(int i) 36 抛出异常的类型: int 37 -1 ==》 参数异常 38 -2 ==》 运行异常 39 -3 ==》 超时异常 40 */ 41 void func(int i) 42 { 43 if( i < 0 ) 44 { 45 throw -1; 46 } 47 48 if( i > 100 ) 49 { 50 throw -2; 51 } 52 53 if( i == 11 ) 54 { 55 throw -3; 56 } 57 58 cout << "Run func..." << endl; 59 } 60 61 void MyFunc(int i) 62 { 63 try 64 { 65 func(i); 66 } 67 catch(int i) 68 { 69 switch(i) 70 { 71 case -1: 72 throw "Invalid Parameter"; 73 break; 74 case -2: 75 throw "Runtime Exception"; 76 break; 77 case -3: 78 throw "Timeout Exception"; 79 break; 80 } 81 } 82 } 83 84 int main(int argc, char *argv[]) 85 { 86 // Demo(); 87 88 try 89 { 90 MyFunc(11); 91 } 92 catch(const char* cs) 93 { 94 cout << "Exception Info: " << cs << endl; 95 } 96 97 return 0; 98 }
运行结果如下:
如何使异常的信息更加丰富呢?
类类型的异常示例:
1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 6 class Base 7 { 8 }; 9 10 class Exception : public Base 11 { 12 int m_id; 13 string m_desc; 14 public: 15 Exception(int id, string desc) 16 { 17 m_id = id; 18 m_desc = desc; 19 } 20 21 int id() const 22 { 23 return m_id; 24 } 25 26 string description() const 27 { 28 return m_desc; 29 } 30 }; 31 32 33 /* 34 假设: 当前的函数式第三方库中的函数,因此,我们无法修改源代码 35 36 函数名: void func(int i) 37 抛出异常的类型: int 38 -1 ==》 参数异常 39 -2 ==》 运行异常 40 -3 ==》 超时异常 41 */ 42 void func(int i) 43 { 44 if( i < 0 ) 45 { 46 throw -1; 47 } 48 49 if( i > 100 ) 50 { 51 throw -2; 52 } 53 54 if( i == 11 ) 55 { 56 throw -3; 57 } 58 59 cout << "Run func..." << endl; 60 } 61 62 void MyFunc(int i) 63 { 64 try 65 { 66 func(i); 67 } 68 catch(int i) 69 { 70 switch(i) 71 { 72 case -1: 73 throw Exception(-1, "Invalid Parameter"); 74 break; 75 case -2: 76 throw Exception(-2, "Runtime Exception"); 77 break; 78 case -3: 79 throw Exception(-3, "Timeout Exception"); 80 break; 81 } 82 } 83 } 84 85 int main(int argc, char *argv[]) 86 { 87 try 88 { 89 MyFunc(11); 90 } 91 catch(const Exception& e) 92 { 93 cout << "Exception Info: " << endl; 94 cout << " ID: " << e.id() << endl; 95 cout << " Description: " << e.description() << endl; 96 } 97 catch(const Base& e) 98 { 99 cout << "catch(const Base& e)" << endl; 100 } 101 102 return 0; 103 }
C++标准库中提供了实用的异常类族
标准库中异常使用:
Array.h
1 #ifndef _ARRAY_H_ 2 #define _ARRAY_H_ 3 4 #include <stdexcept> 5 6 using namespace std; 7 8 template 9 < typename T, int N > 10 class Array 11 { 12 T m_array[N]; 13 public: 14 int length() const; 15 bool set(int index, T value); 16 bool get(int index, T& value); 17 T& operator[] (int index); 18 T operator[] (int index) const; 19 virtual ~Array(); 20 }; 21 22 template 23 < typename T, int N > 24 int Array<T, N>::length() const 25 { 26 return N; 27 } 28 29 template 30 < typename T, int N > 31 bool Array<T, N>::set(int index, T value) 32 { 33 bool ret = (0 <= index) && (index < N); 34 35 if( ret ) 36 { 37 m_array[index] = value; 38 } 39 40 return ret; 41 } 42 43 template 44 < typename T, int N > 45 bool Array<T, N>::get(int index, T& value) 46 { 47 bool ret = (0 <= index) && (index < N); 48 49 if( ret ) 50 { 51 value = m_array[index]; 52 } 53 54 return ret; 55 } 56 57 template 58 < typename T, int N > 59 T& Array<T, N>::operator[] (int index) 60 { 61 if( (0 <= index) && (index < N) ) 62 { 63 return m_array[index]; 64 } 65 else 66 { 67 throw out_of_range("T& Array<T, N>::operator[] (int index)"); 68 } 69 } 70 71 template 72 < typename T, int N > 73 T Array<T, N>::operator[] (int index) const 74 { 75 if( (0 <= index) && (index < N) ) 76 { 77 return m_array[index]; 78 } 79 else 80 { 81 throw out_of_range("T Array<T, N>::operator[] (int index) const"); 82 } 83 } 84 85 template 86 < typename T, int N > 87 Array<T, N>::~Array() 88 { 89 90 } 91 92 #endif
HeepArray.h
1 #ifndef _HEAPARRAY_H_ 2 #define _HEAPARRAY_H_ 3 4 #include <stdexcept> 5 6 using namespace std; 7 8 template 9 < typename T > 10 class HeapArray 11 { 12 private: 13 int m_length; 14 T* m_pointer; 15 16 HeapArray(int len); 17 HeapArray(const HeapArray<T>& obj); 18 bool construct(); 19 public: 20 static HeapArray<T>* NewInstance(int length); 21 int length() const; 22 bool get(int index, T& value); 23 bool set(int index ,T value); 24 T& operator [] (int index); 25 T operator [] (int index) const; 26 HeapArray<T>& self(); 27 const HeapArray<T>& self() const; 28 ~HeapArray(); 29 }; 30 31 template 32 < typename T > 33 HeapArray<T>::HeapArray(int len) 34 { 35 m_length = len; 36 } 37 38 template 39 < typename T > 40 bool HeapArray<T>::construct() 41 { 42 m_pointer = new T[m_length]; 43 44 return m_pointer != NULL; 45 } 46 47 template 48 < typename T > 49 HeapArray<T>* HeapArray<T>::NewInstance(int length) 50 { 51 HeapArray<T>* ret = new HeapArray<T>(length); 52 53 if( !(ret && ret->construct()) ) 54 { 55 delete ret; 56 ret = 0; 57 } 58 59 return ret; 60 } 61 62 template 63 < typename T > 64 int HeapArray<T>::length() const 65 { 66 return m_length; 67 } 68 69 template 70 < typename T > 71 bool HeapArray<T>::get(int index, T& value) 72 { 73 bool ret = (0 <= index) && (index < length()); 74 75 if( ret ) 76 { 77 value = m_pointer[index]; 78 } 79 80 return ret; 81 } 82 83 template 84 < typename T > 85 bool HeapArray<T>::set(int index, T value) 86 { 87 bool ret = (0 <= index) && (index < length()); 88 89 if( ret ) 90 { 91 m_pointer[index] = value; 92 } 93 94 return ret; 95 } 96 97 template 98 < typename T > 99 T& HeapArray<T>::operator [] (int index) 100 { 101 if( (0 <= index) && (index < length()) ) 102 { 103 return m_pointer[index]; 104 } 105 else 106 { 107 throw out_of_range("T& HeapArray<T>::operator [] (int index)"); 108 } 109 } 110 111 template 112 < typename T > 113 T HeapArray<T>::operator [] (int index) const 114 { 115 if( (0 <= index) && (index < length()) ) 116 { 117 return m_pointer[index]; 118 } 119 else 120 { 121 throw out_of_range("T HeapArray<T>::operator [] (int index) const"); 122 } 123 } 124 125 template 126 < typename T > 127 HeapArray<T>& HeapArray<T>::self() 128 { 129 return *this; 130 } 131 132 template 133 < typename T > 134 const HeapArray<T>& HeapArray<T>::self() const 135 { 136 return *this; 137 } 138 139 template 140 < typename T > 141 HeapArray<T>::~HeapArray() 142 { 143 delete[]m_pointer; 144 } 145 146 147 #endif
主程序:
1 #include <iostream> 2 #include <string> 3 #include "Array.h" 4 #include "HeapArray.h" 5 6 using namespace std; 7 8 void TestArray() 9 { 10 Array<int, 5> a; 11 12 for(int i=0; i<a.length(); i++) 13 { 14 a[i] = i; 15 } 16 17 for(int i=0; i<a.length(); i++) 18 { 19 cout << a[i] << endl; 20 } 21 } 22 23 void TestHeapArray() 24 { 25 HeapArray<double>* pa = HeapArray<double>::NewInstance(5); 26 27 if( pa != NULL ) 28 { 29 HeapArray<double>& array = pa->self(); 30 31 for(int i=0; i<array.length(); i++) 32 { 33 array[i] = i; 34 } 35 36 for(int i=0; i<array.length(); i++) 37 { 38 cout << array[i] << endl; 39 } 40 } 41 42 delete pa; 43 } 44 45 int main(int argc, char *argv[]) 46 { 47 48 try 49 { 50 TestArray(); 51 52 cout << endl; 53 54 TestHeapArray(); 55 } 56 catch(...) 57 { 58 cout << "Exception" << endl; 59 } 60 61 return 0; 62 }
运行结果:
小结:
原文:https://www.cnblogs.com/wanmeishenghuo/p/9595198.html