C++try/catch(编程新手,关于这个子句的信息不多)



我是编程新手,在try/catch子句方面遇到了问题。

这是我的一本教科书中的一个例子:

int main( ) 
{
   char *ptr;
   try {
      ptr = new char[ 1000000000 ];
   }
   catch( … ) {
      cout << "Too many elements" << endl;
   }
   return 0;
}

我试着在网上寻找进一步的解释,但教科书并没有确切地告诉我这些条款的实际作用或用途。

任何信息都会有所帮助。

编辑:我正在使用的课本是:C++:类和数据结构

try-catch是用于异常处理的C++构造。谷歌"C++例外"。

Try-catch是一种处理异常的方法:

try
{
      // Do work in here
      // If any exceptions are generated then the code in here is stopped.
      // and a jump is made to the catch block.
      // to see if the exception can be handled.
      // An exception is generated when somebody uses throw.
      // Either you or one of the functions you call.
      // In your case new can throw std::bad_alloc
      // Which is derived from std::runtime_error which is derived from std::exception
}
// CATCH BLOCK HERE.

catch块用于定义要处理的异常。

// CATCH BLOCK
catch(MyException const& e)
{
     // Correct a MyException
}
catch(std::exception const& e)
{
     // Correct a std::exception
     // For example this would cat any exception derived from std::exception
}

你可以有任意多的接球块。如果异常匹配catch语句中的任何catch表达式,则执行关联的代码块。如果没有catch表达式与异常匹配,则堆栈将展开,直到找到更高级别的catch块,并重复处理(如果找不到匹配的catch区块,则可能导致应用程序退出)。

注意:如果多个catch表达式匹配,则使用词汇上的第一个。将只执行一个或不执行任何catch块。如果没有,则编译器将查找更高级别的try/catch。

还有一个捕获任何东西的条款

catch(...)
{
     // This is a catch all.
     // If the exception is not listed above this will catch any exception.
}

那么这是如何应用于您的代码的呢。

int main( ) 
{
   char *ptr;
   try
   {
      // This calls ::new() which can potentially throw std::bad_alloc
      // If this happens then it will look for a catch block.
      ptr = new char[ 1000000000 ];

     // If the ::new() works then nothing happens and you pointer `ptr`
     // is valid and code continues to execute. 
   }
   catch( … )
   {
      // You only have one catch block that catches everything.
      // So if there are any statements that generate an exception this will catch
      // the excetption and execute this code.
      cout << "Too many elements" << endl;
   }
   // As you have caught all exceptions the code will continue from here.
   // Either after the try block finishes successfully or
   // After an exception has been handled by the catch block.
   return 0;
}

Try-catch块用于捕获代码中的错误。

在最基本的层面上,错误的发生是因为程序试图执行无效的指令。由于多种原因,该指令(读取:代码行)可能无效。在您的特定实例中,如果您的程序无法分配1000000000字节的内存来编写ptr,则该指令可能无效。最常见的异常是试图访问坏指针,这被称为空指针异常,当您试图对尚未创建、已删除(或已损坏)的对象执行某些操作时,就会发生这种异常。你将学会讨厌这种例外。

使用catch(…)告诉程序,如果try块内的代码内发生任何错误,则执行catch块内的程序。在那里,您可以处理错误,并尝试找到某种方法来修复错误条件或优雅地退出该模块。

您还可以捕捉特定的错误,可以在此处了解更多信息:http://www.cplusplus.com/doc/tutorial/exceptions/

如果您已经了解C,try/catch在用于错误处理时可以实现与setjmp/longjmp相同的功能。把try看作setjmp的if条件的代码,把catch看作setjmp的else条件的代码。这使得longjmp等价于C++中用于抛出异常的throw。在您的示例中,内部调用某些内存分配函数的new运算符可能会在使用C++throw运算符看到一个非常大的数字作为输入时抛出异常。

void a()
{
      .......
      longjmp(buf,1);      // <--- similar to throw
      .......
}
if ( !setjmp(buf) )        // <--- similar to try
{
      .......
      a(); 
      .......                
} 
else                       // <--- similar to catch
{                    
      .......          
}

try/catchsetjmp/longjmp复杂一点,对于setjmp/longjmp,您需要将在setjmp/longjmp调用之间修改的变量声明为volatile,这对于try/catch来说是不必要的。

最新更新