c++中 try 和catch的用法

今天看了msdn,里面有个
catch(object^) 什么意思。。。。
我想要测试一个堆栈弹出空值是否会报错,如果没有报错,我要把它打印出来。问题在于,我发现这个堆栈他没有报错,而我要把这个错误打印出来的时候,程序他出一个框说程序出错了,所以下一句(打印命令)无法执行。
请问可以利用try和catch直接捕获这个出错,让他不中止程序好让我执行下一条命令(打印出下一句话)吗。
那要用什么方法?

1、在c++中,可以直接抛出异常之后自己进行捕捉处理,如:(这样就可以在任何自己得到不想要的结果的时候进行中断,比如在进行数据库事务操作的时候,如果某一个语句返回SQL_ERROR则直接抛出异常,在catch块中进行事务回滚,用法:

#include <iostream>  #include <exception>  

using namespace std;  

int main () {

try

{

throw 1;

throw "error";

}

catch(char *str)

{

cout << str << endl;

}

catch(int i)

{

cout << i << endl;

}  

}  

2、 try 和catch定义异常类来进行处理:

#include <iostream>  #include <exception>  

using namespace std;

//可以自己定义Exception  class myexception: public exception  

{

virtual const char* what() const throw()

{

return "My exception happened";

}  

}myex;

int main () {

try

{

if(true)    //如果,则抛出异常;

throw myex;

}

catch (exception& e)

{

cout << e.what() << endl;

}

return 0;  

扩展资料

c++的特点:

1、C++通过建立用户定义类支持数据封装和数据隐藏。

2、C++是支持数据封装的工具,对象则是数据封装的实现。

3、采用多态性为每个类指定表现行为。多态性形成由父类和它们的子类组成的一个树型结构。

4、C++现有类的基础上可以声明新类型,这就是继承和重用的思想。通过继承和重用可以更有效地组织程序结构,明确类间关系,并且充分利用已有的类来完成更复杂、深入的开发。新定义的类为子类,成为派生类。

参考资料来源:百度百科—C++

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-03-15

  一、简单的例子

  单刀直入,首先通过一个简单的例子来看基本的用法。

#include<iostream.h>                            //包含头文件
#include<stdlib.h>
double fuc(double x, double y)                        //定义函数
{
if(y==0)
{
throw y;                                    //除数为0,抛出异常
}
return x/y;                                    //否则返回两个数的商
}
void main()
{
double res;
try                                            //定义异常
{
res=fuc(2,3);
cout<<"The result of x/y is : "<<res<<endl;
res=fuc(4,0);                                //出现异常
}
catch(double)                                    //捕获并处理异常
{
 cerr<<"error of dividing zero.\n";
  exit(1);                                    //异常退出程序
 }
}

  catch 的数据类型需要与throw出来的数据类型相匹配的。

  

  二、catch(...)的作用

  catch(…)能够捕获多种数据类型的异常对象,所以它提供给程序员一种对异常对象更好的控制手段,使开发的软件系统有很好的可靠性。因此一个比较有经验的程序员通常会这样组织编写它的代码模块,如下:

void Func()
{
try
{
    // 这里的程序代码完成真正复杂的计算工作,这些代码在执行过程中
    // 有可能抛出DataType1、DataType2和DataType3类型的异常对象。
}
catch(DataType1& d1)
{
}
catch(DataType2& d2)
{
}
catch(DataType3& d3)
{
}
// 注意上面try block中可能抛出的DataType1、DataType2和DataType3三
// 种类型的异常对象在前面都已经有对应的catch block来处理。但为什么
// 还要在最后再定义一个catch(…) block呢?这就是为了有更好的安全性和
// 可靠性,避免上面的try block抛出了其它未考虑到的异常对象时导致的程
// 序出现意外崩溃的严重后果,而且这在用VC开发的系统上更特别有效,因
// 为catch(…)能捕获系统出现的异常,而系统异常往往令程序员头痛了,现
// 在系统一般都比较复杂,而且由很多人共同开发,一不小心就会导致一个
// 指针变量指向了其它非法区域,结果意外灾难不幸发生了。catch(…)为这种
// 潜在的隐患提供了一种有效的补救措施。
catch(…)
{
}
}

  三、异常中采用面向对象的处理

  首先看下面的例子:

void OpenFile(string f)
{
try
{
   // 打开文件的操作,可能抛出FileOpenException
}
catch(FileOpenException& fe)
{
   // 处理这个异常,如果这个异常可以很好的得以恢复,那么处理完毕后函数
   // 正常返回;否则必须重新抛出这个异常,以供上层的调用函数来能再次处
   // 理这个异常对象
   int result = ReOpenFile(f);
   if (result == false) throw;
}
}
void ReadFile(File f)
{
try
{
   // 从文件中读数据,可能抛出FileReadException
}
catch(FileReadException& fe)
{
   // 处理这个异常,如果这个异常可以很好的得以恢复,那么处理完毕后函数
   // 正常返回;否则必须重新抛出这个异常,以供上层的调用函数来能再次处
   // 理这个异常对象
   int result = ReReadFile(f);
   if (result == false) throw;
}
}
void WriteFile(File f)
{
try
{
    // 往文件中写数据,可能抛出FileWriteException
}
catch(FileWriteException& fe)
{
    // 处理这个异常,如果这个异常可以很好的得以恢复,那么处理完毕后函数
// 正常返回;否则必须重新抛出这个异常,以供上层的调用函数来能再次处理这个异对象
    int result = ReWriteFile(f);
    if (result == false) throw; 

}
void Func()
{
try
{
   // 对文件进行操作,可能出现FileWriteException、FileWriteException
   // 和FileWriteException异常
   OpenFile(…);
   ReadFile(…);
   WriteFile(…);
}
// 注意:FileException是FileOpenException、FileReadException和FileWriteException
// 的基类,因此这里定义的catch(FileException& fe)能捕获所有与文件操作失败的异
// 常。
catch(FileException& fe)
{
   ExceptionInfo* ef = fe.GetExceptionInfo();
   cout << “操作文件时出现了不可恢复的错误,原因是:”<< fe << endl;
}
}

  下面是更多面向对象和异常处理结合的例子:

#include <iostream.h>
class ExceptionClass
{
    char* name;
public:
    ExceptionClass(const char* name="default name") 
    {
             cout<<"Construct "<<name<<endl;
             this->name=name;
    }
   ~ExceptionClass()
    {
             cout<<"Destruct "<<name<<endl;
    }
    void mythrow()
   {
            throw ExceptionClass("my throw");
   }
}
void main()
{
       ExceptionClass e("Test");
       try
       {
           e.mythrow();
       }  
       catch(...)
      {
         cout<<”*********”<<endl;
       }
}

  这是输出信息:

  Construct Test

  Construct my throw

  Destruct my throw

  ****************

  Destruct my throw   (这里是异常处理空间中对异常类的拷贝的析构)

  Destruct Test

  ======================================

  不过一般来说可能更习惯于把会产生异常的语句和要throw的异常类分成不同的类来写,下面的代码可以是更愿意书写的:

class ExceptionClass
{
public:
    ExceptionClass(const char* name="Exception Default Class")
   {
       cout<<"Exception Class Construct String"<<endl;
   }
   ~ExceptionClass()
   {
      cout<<"Exception Class Destruct String"<<endl;
   }
   void ReportError()
   {
      cout<<"Exception Class:: This is Report Error Message"<<endl;
   }
};
class ArguClass
{
   char* name;
public:
   ArguClass(char* name="default name")
   {
      cout<<"Construct String::"<<name<<endl;
      this->name=name;
   }
   ~ArguClass()
   {
      cout<<"Destruct String::"<<name<<endl;
   }
   void mythrow()
   {
      throw ExceptionClass("my throw");
   }      
};
_tmain()
{
   ArguClass e("haha");
   try
   {
     e.mythrow();
   }
   catch(int)
   {
     cout<<"If This is Message display screen, This is a Error!!"<<endl;  //这行不会执行
   }
   catch(ExceptionClass pTest)
   {
      pTest.ReportError();
   }
   catch(...)
  {
       cout<<"***************"<<endl;  
  }
}

  输出Message:

  Construct String::haha

  Exception Class Construct String

  Exception Class Destruct String

  Exception Class:: This is Report Error Message

  Exception Class Destruct String

  Destruct String::haha

本回答被网友采纳
第2个回答  推荐于2017-09-30
catch(object^)表示捕获一个object类型的异常类。而这个类必须是在try语句块中被抛出的。例如:
#include<iostream>
#include<string>
using namespace std;

int main()
{
string ex = "this is a exception";
try
{
cout<<"before throw"<<endl;
throw ex;
cout<<"after throw"<<endl;
}
catch(string &e)
{
cout<<e<<endl;
}

cout<<"end"<<endl;
}

以上这个例子也说明了,你这个问题用try,catch是不能解决的。捕捉到异常后,是不可能再执行下一条语句,当然如果你放在catch语句块之后,是可以执行。

将抛出异常的语句放在try中,在catch中处理该异常,在catch之后继续执行就可以了。本回答被提问者采纳
第3个回答  2018-03-15

针对最佳答案答主的回答 后面这两个例子的输出信息是错误的 希望初学者们自己运行下看看 不要被误导

倒数第二个例子catch捕获的是所有异常,所以并不会进行两次析构,如果catch的异常类型是ExceptionClass  并且声明了对象,才会两次析构

倒数第一个例子析构的位置不对,先调用pTest.ReportError()之后才会进行两次析构

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网