C++ C++;try/catch(编程新手,关于此子句的信息不多)
我是编程新手,在try/catch子句方面遇到了问题 以下是我拥有的一本教科书中的一个例子:C++ C++;try/catch(编程新手,关于此子句的信息不多),c++,C++,我是编程新手,在try/catch子句方面遇到了问题 以下是我拥有的一本教科书中的一个例子: int main( ) { char *ptr; try { ptr = new char[ 1000000000 ]; } catch( … ) { cout << "Too many elements" << endl; } return 0; } int main() { char*ptr; 试一试{ p
int main( )
{
char *ptr;
try {
ptr = new char[ 1000000000 ];
}
catch( … ) {
cout << "Too many elements" << endl;
}
return 0;
}
int main()
{
char*ptr;
试一试{
ptr=新字符[100000000];
}
捕获(…){
CUT一个尝试捕获是C++异常处理的构造。谷歌的C++异常。
< P> catch是C++异常处理的构造。谷歌C++异常。 < P>尝试catch块用于捕获代码中的错误。< /P>
在最基本的层次上,错误发生是因为程序试图执行无效的指令可能因多种原因而无效。在您的特定实例中,如果您的程序无法分配100000000字节的内存来记录ptr,则该指令可能无效。最常见的异常是尝试访问错误指针,称为空指针异常,当您尝试对其执行某些操作时会发生此异常尚未创建或已删除(或已损坏)的对象。您将学会讨厌该异常
使用catch(…)告诉程序,如果try块中的代码出现任何错误,则在catch块中执行代码。在那里,您可以处理错误并尝试找到某种方法来修复错误条件或优雅地退出该模块
您还可以捕获特定错误,您可以在此处找到更多信息:Try catch块用于捕获代码中的错误
在最基本的层次上,错误发生是因为程序试图执行无效的指令可能因多种原因而无效。在您的特定实例中,如果您的程序无法分配100000000字节的内存来记录ptr,则该指令可能无效。最常见的异常是尝试访问错误指针,称为空指针异常,当您尝试对其执行某些操作时会发生此异常尚未创建或已删除(或已损坏)的对象。您将学会讨厌该异常
使用catch(…)告诉程序,如果try块中的代码出现任何错误,则在catch块中执行代码。在那里,您可以处理错误并尝试找到某种方法来修复错误条件或优雅地退出该模块
您还可以捕获特定的错误,您可以在这里找到更多信息:如果您已经了解C,try/catch
在用于错误处理时实现与setjmp/longjmp
相同的功能。将try
看作setjmp
的If条件的代码和catch
用于setjmp的else的代码这使得C++中的LangJMP 等价于C++中的代码>抛出< /C> >。在例子中,可能是,<代码>新< /Cuff>运算符,它调用内部的内存分配函数,使用C++<代码>抛出< /Cult>运算符,抛出一个非常大的数字作为例外。
void a()
{
.......
longjmp(buf,1); // <--- similar to throw
.......
}
if ( !setjmp(buf) ) // <--- similar to try
{
.......
a();
.......
}
else // <--- similar to catch
{
.......
}
void a()
{
.......
longjmp(buf,1)//如果你已经知道C,那么当用于错误处理时,try/catch
实现了与setjmp/longjmp
相同的功能。将try
看作是setjmp
的If条件的代码和catch
的else的代码。这使得longjmp
相当于throw
在C++中,它用来引发异常。在例子中,可能是“代码> >新< /Cord>运算符,它调用内部的内存分配函数,使用C++ +代码>抛出< /COD>运算符,将异常大数输入为异常。< /P>
void a()
{
.......
longjmp(buf,1); // <--- similar to throw
.......
}
if ( !setjmp(buf) ) // <--- similar to try
{
.......
a();
.......
}
else // <--- similar to catch
{
.......
}
void a()
{
.......
longjmp(buf,1);//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表达式匹配,则使用词汇上的第一个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;
}
int main()
{
char*ptr;
尝试
{
//这将调用::new(),它可能会抛出std::bad_alloc
//如果发生这种情况,它将寻找一个捕捉块。
ptr=新字符[100000000];
//如果::new()有效,那么什么也不会发生,您可以使用指针`ptr`
//是有效的,代码将继续执行。
}
捕获(…)
{
//您只有一个捕捉块可以捕捉所有内容。
//因此,如果有任何语句生成异常,这将捕获
//执行此代码。
coutTry 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表达式匹配,则使用词汇上的第一个catch表达式。只执行一个catch块或不执行任何catch块。如果没有,则编译器将查找