c++throw(c++throw catch)
本篇文章给大家谈谈c++throw,以及c++throw catch对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、C+编程问题:try,catch,throw代码不起作用?
- 2、C++里 throw后面没有参数(表达式)会怎么样
- 3、c语言退出一个函数除了用return 还有什么方法
- 4、关于C++里面throw的用法
- 5、C++异常必须要显示throw吗?不throw时候是怎么处理的呢?
- 6、c/c++ throw
C+编程问题:try,catch,throw代码不起作用?
很正常,这句throw
22;不起作用,,因为雹颂你在下面代码中
只捕获int类数蔽型的异常,而没有捕获其他类型的异常源毕郑。当异常堆栈展开的时间,由于没有找到空指针类型的异常,程序就将控制权交给了系统处理了,因此throw
22;//根本就没有执行到这里。
[img]C++里 throw后面没有参数(表达式)会怎么样
C++ exception handling uses the try, catch, and throw statements to implement exception handling. With C++ exception handling, your program can communicate unexpected events to a higher execution context that is better able to recover from such abnormal events. These exceptions are handled by code that is outside the normal flow of control.
Note The Win32 structured exception-handling mechanism works with both C and C++ source files. However, it is not specifically designed for C++. You can ensure that your code is more portable by using C++ exception handling. Also, C++ exception handling is more flexible, in that it can handle exceptions of any type. For C++ programs, it is recommended that you use the C++ exception-handling mechanism (try, catch, throw) described in this topic.
try-block :
try compound-statement handler-list
handler-list :
handler handler-listopt
handler :
catch ( exception-declaration ) compound-statement
exception-declaration :
type-specifier-list declarator
type-specifier-list abstract-declarator
type-specifier-list
...
throw-expression :
throw assignment-expressionopt
The compound-statement after the try clause is the guarded section of code. The throw-expression “throws” (raises) an exception. The compound-statement after the catch clause is the exception handler, and “catches” (handles) the exception thrown by the throw-expression. The exception-declaration statement indicates the type of exception the clause handles. The type can be any valid data type, including a C++ class. If the exception-declaration statement is an ellipsis (...), the catch clause handles any type of exception, including a C exception. Such a handler must be the last handler for its try-block.
The operand of throw is syntactically similar to the operand of a return statement.
Note Microsoft C++ does not support exception-specifications, as described in section 15.4 of the ANSI C++ draft. In addition, it does not support function-try-block described in section 15 of the ANSI C++ draft.
For more information on C++ exception handling, see Exception Handling Topics (C++). For information on exception handling in general, see Exception Handling: Overview.
END C++ Specific
Example
In the following example, the try block attempts to write the headers. The catch block then handles a specific file exception, and passes all other exceptions on to the outer block with the throw macro:
// Example of the try and catch statements
try
{
// Write the file header
file.Write((LPSTR)bmfHdr, sizeof(BITMAPFILEHEADER));
//
// Write the DIB header and the bits
file.WriteHuge(lpBI, dwDIBSize);
}
catch (CFileException* e)
{
::GlobalUnlock((HGLOBAL) hDib);
throw;
}
::GlobalUnlock((HGLOBAL) hDib);
return TRUE;
c语言退出一个函数除了用return 还有什么方法
没有谈裂贺了
类似的只有函数自然含派结束的最后一个},也表示函数源弯退出
再么强制中止程序的exit(0);偶尔会用它来做错误处理,结束程序。
关于C++里面throw的用法
你这个在哪儿catch的啊?只throw不catch真的没问题嘛,一路丢出去的节奏啊
C++异常必须要显示throw吗?不throw时候是怎么处理的呢?
除零错误这样的异常,在Windows操作系统上,其实最先是由处理器饥猜抛出中断,然后转化为操作系统的异常。默认情况下,Windows的异常要用SEH的方式来处理,C++异常并不唤肢乱能捕获到Win32异常,这是两个概念,虽然都叫异常。
想要在Win32异常发生时自动转化为C++异常,需要修改VS的工程设置,打开这个转化(默认是关和档闭的),然后就可以用C++异常的捕获方式拿到Win32异常了。但只能通过...的方式捕获,不能拿到更一步的异常信息。要想拿到更多的信息,必须使用SEH异常捕获方式 __try __except 的方式。
c/c++ throw
throw是用来销世抛出异常的。与之对应的处理异常的关键字还有try catch。throw一般扰顷是要被放在try语块中的用来抛出异常,而在对应的catch语块中进行相应异常的处理。
用你上面的那个strcpy举个例子吧:
========================================
#include stdio.h
#include string.h
char* _strcpy(char *a, const char *s)
{
try
{
char *temp;
if (NULL == a || NULL == s)
throw "Invalid argument(s)"; //这里抛出一个字符串异常(char*)类型的
temp = a;
while(*a++ = *s++)
{}
return temp;
}
catch(char* e) //在这里接住这个char*类型的异常,e就是指向异常对象:"Invalid argument(s)"
{
printf("%s", e);
}
}
int main(int argc, char* argv[])
{
char a[32];
_strcpy(a, NULL);
return 0;
}
======================================
运行结果:
Invalid argument(s)
======================================
上面这种抛出C风格的字符串的情况比较少见,一般都是抛出一缓斗陆个异常类型的对象。再把上面的_strcpy函数改一下,用string类做为一个异常对象:
char* _strcpy(char *a, const char *s)
{
try
{
char *temp;
if (NULL == a || NULL == s)
throw string("Invalid argument(s)");
temp = a;
while(*a++ = *s++)
{}
return temp;
}
catch(string e)
{
printf("%s\n", e.c_str());
}
}
关于异常更加深入详细的介绍,请参看我空间中的这篇文章:
如果还有问题,欢迎探讨。
关于c++throw和c++throw catch的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。