C++中throw异常

<span style="font-size:18px;">throw是C++中关键字,用来抛出异常。不使用关键字throw,try就什么异常也捕获不了,throw的使用语法规则:throw data; </span>
<span style="font-size:18px;"> data为定义的异常数据类型.throw抛出异常通常用来解决,内存空间不足,数组下标,数据类型初始化数值类型不对。</span>
<span style="font-size:18px;">例如char *q=0;  throw 0;
数组为10个,输入数据为11个。</span>

#include <iostream>
#include <string> 
using namespace std;
class Student
 {
 public:
      Student(int n,string nam) 
       {
	   cout<<"constructor-"<<n<<endl;
       num=n;name=nam;
	   }
   ~Student()
   {
   cout<<"destructor-"<<num<<endl;
   }
void get_data();
   private:
      int num;
      string name;
 };
void Student::get_data()
 {
   if(num==0) throw num;
    else cout<<num<<" "<<name<<endl;
    cout<<"in get_data()"<<endl;
 }

void fun()
{
 Student stud1(1101,"tan");
 stud1.get_data();
 try
  {
    Student stud2(0,"Li");
    stud2.get_data();
  }
 catch
  (int n)
  {cout<<"num="<<n<<",error!"<<endl;}
  
}
int main()
{
 cout<<"main begin"<<endl;
 cout<<"call fun()"<<endl;
 fun();
 cout<<"main end"<<endl;
 return 0;
}
<span style="font-size:18px;">在上面程序中,第一个函数fun中,先执行了stud1的构造函数....</span>
<span style="font-size:18px;">而在执行stud2时,构造完之后,num=0,抛出异常在catch中解决掉解决办法在这个函数中是delete掉这个stud2的初始化。</span>
<span style="font-size:18px;">运行结果如图所示.</span>
<p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;"><img src="https://img-blog.csdn.net/20160908170926035" style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;" alt="" /></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">对于抽象数据类型的异常对象。catch(…)同样有效,例程如下:</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">class MyException
{
public:
protected:
int code;
};</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">int main()
{
try
{
cout << "在 try block 中, 准备抛出一个异常." << endl;
//这里抛出一个异常(其中异常对象的数据类型是MyException)
throw MyException();
}
//catch(MyException& value )
//注意这里catch语句
catch( …)
{
cout << "在catch(…) block中, MyException类型的异常对象被处理" << endl;
}
}</p>
<p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">一.int类型的异常被catch(…)抓获了,再来另一个例子:</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">int main()
{
try
{
cout << "在 try block 中, 准备抛出一个异常." << endl;
//这里抛出一个异常(其中异常对象的数据类型是double,值为0.5)
throw 0.5;
}
//catch( double& value )
//注意这里catch语句
catch( …)
{
cout << "在 catch(…) block 中, double类型的异常对象也被处理" << endl;
}
}</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">二.对于抽象数据类型的异常对象。catch(…)同样有效,例程如下:</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">class MyException
{
public:
protected:
int code;
};</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;">int main()
{
try
{
cout << "在 try block 中, 准备抛出一个异常." << endl;
//这里抛出一个异常(其中异常对象的数据类型是MyException)
throw MyException();
}
//catch(MyException& value )
//注意这里catch语句
catch( …)
{
cout << "在catch(…) block中, MyException类型的异常对象被处理" << endl;
}
}</p>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值