模板类与异常

 1 /* 模板处理异常*/
 2 
 3 #include<iostream>
 4 
 5 using namespace std;
 6 
 7 template<class T>
 8 class myarray
 9 {
10 public:
11     myarray(int num)
12     {
13         if(num < 0)
14         {
15             //throw error();
16             throw Terror<T>();
17         }
18         this->n = num;
19         this->p = new T[n];
20     }
21 
22     ~myarray()
23     {
24         delete []this->p;
25     }
26     
27     class error
28     {
29     };
30     
31     template<class T>
32     class Terror
33     {
34     public:
35 
36     };
37 
38 private:
39     T *p;
40     int n;
41 };
42 
43 void main()
44 {    
45     // 模板类的异常必须捕获的时候明确类型
46     try
47     {
48         myarray<int> myint(10);
49     }
50     catch (myarray<int>::error e)
51     {
52         cout << typeid(e).name() << endl;
53     }
54     catch (myarray<int>::Terror<int> e)
55     {
56         cout << typeid(e).name() << endl;
57     }
58 
59     cin.get();
60 }

 

posted on 2015-06-16 14:38  Dragon-wuxl  阅读(140)  评论(0)    收藏  举报

导航