C++try/catch(编程新手,关于这个子句的信息不多)
C++ try / catch (new to programming and not much info about this clause)
我是编程新手,在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/catch
比setjmp/longjmp
复杂一点,对于setjmp/longjmp
,您需要将在setjmp/longjmp
调用之间修改的变量声明为volatile,这对于try/catch
来说是不必要的。
- 正在查找文档以获得PS4平台的C++中的设备信息
- 如何理解C++标准N3337中的expr.const.cast子句8
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 如何设置一个范围来提取我想要获得的信息
- 系统参数信息A 与 SPI_GETMOUSE 返回 0
- 为什么 gcovr 会生成空覆盖率统计信息?
- if 子句中的赋值不起作用
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- C++20 概念:如何在"requires"子句中引用类名?
- 尝试将另一个子句添加到代码中时出错
- 对OpenMP reduction子句中的变量执行原子操作
- 将信息输入到下面显示的结构向量中的正确语法/格式是什么
- 调试符号中缺少 QObject 类信息(编辑但存在其他 Qt 类)
- 为什么数组大小信息可用于"sizeof"运算符和 delete[] 运算符,但在将数组作为参数传递到
- Pisarze - 来自波兰奥林匹克信息学的数据分析任务
- 将数组信息存储到 c++ 向量中有一个"Access violation reading location"
- C++20 个模块在 clang (Windows):在最简单的示例中键入信息错误
- C++ 如何暂停控制台输出,以便用户可以在继续之前阅读信息
- C++try/catch(编程新手,关于这个子句的信息不多)