throw
【抛出异常】(也称为抛弃异常)即检测是否产生异常,在C++中,其采用throw语句来实现,如果检测到产生异常,则抛出异常。该语句的格式为:
throw 表枣敏达式
如果在try语句块的程序段中(包括在其中调用的函数)发现了异常,且抛弃了该异常,则这个异常就可以被try语句块后的某个catch语句所捕获并处理,捕获和处理的条件是被抛弃的异常的类型与catch语句的异常类型相匹配。由于C++使用数据类型来区分不同的异常,因此在判断异常时,throw语句中的表达式的值就没有实际意义,而表达式的类型就特别重要。
【范例】处理除数为0的异常。该范例将上述除数为0的异常可以用try/catch语句来捕获异常,并使用throw语句来抛出异常,从而实现异常处理,实现代码如代码清单20-2所示。
代码清单20-2
#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) 凳猜枝 //异常退出程序
}
}
【运行结果】在Visual C++中新建一个【C++ Source File】文件,输入上述的代码,编译无误后运行。
【范例解析】上述代码中,在主函数main()的第14~19行中使用了try语句定义异常,其中包含3条有可能出现异常的语句,它们为调用两个数相除的函数。在代码的第兆简20~24行定义了异常处理,即捕获异常后执行该段代码中的语句。此外,在函数fuc()的代码5~8行通过throw语句抛出异常。
任意类型,好告不过需要和你的catch语句类型匹配就行了。你甚至可以可以抛出指针,如
throw (new A)
不过catch需要配对坦漏
catch(A* pA)
{
...
}
常用的类型是 std::exception的派生类让袜烂如 std::logic_error, std::runtime_error
C++ 标准头文件 为了 和 C 标准头文件区分开,所以不用 后缀名重新包装了余亩码 C标准头文件 为 前面加c ,比如 cstdio 基本就是原来的耐隐 stdio.h
cstdio 头文件的内容,包含了 stdio.h ,
但是原来c里面的函数名字先undef,然后重新定义
比如 #undef printf 再using ::printf
C/C++ code
#pragma GCC system_header
#include <bits/c++config.h>
#include <stdio.h>
#ifndef _GLIBCXX_CSTDIO
#define _GLIBCXX_CSTDIO 1
/竖哪/ Get rid of those macros defined in <stdio.h>in lieu of real functions.
#undef clearerr
#undef fclose
#undef feof
#undef ferror
#undef fflush
#undef fgetc
#undef fgetpos
#undef fgets
#undef fopen
#undef fprintf
#undef fputc
#undef fputs
#undef fread
#undef freopen
#undef fscanf
#undef fseek
#undef fsetpos
#undef ftell
#undef fwrite
#undef getc
#undef getchar
#undef gets
#undef perror
#undef printf
#undef putc
#undef putchar
#undef puts
#undef remove
#undef rename
#undef rewind
#undef scanf
#undef setbuf
#undef setvbuf
#undef sprintf
#undef sscanf
#undef tmpfile
#undef tmpnam
#undef ungetc
#undef vfprintf
#undef vprintf
#undef vsprintf
_GLIBCXX_BEGIN_NAMESPACE(std)
using ::FILE
using ::fpos_t
using ::clearerr
using ::fclose
using ::feof
using ::ferror
using ::fflush
using ::fgetc
using ::fgetpos
using ::fgets
using ::fopen
using ::fprintf
using ::fputc
using ::fputs
using ::fread
using ::freopen
using ::fscanf
using ::fseek
using ::fsetpos
using ::ftell
using ::fwrite
using ::getc
using ::getchar
using ::gets
using ::perror
using ::printf
using ::putc
using ::putchar
using ::puts
using ::remove
using ::rename
using ::rewind
using ::scanf
using ::setbuf
using ::setvbuf
using ::sprintf
using ::sscanf
using ::tmpfile
using ::tmpnam
using ::ungetc
using ::vfprintf
using ::vprintf
using ::vsprintf
_GLIBCXX_END_NAMESPACE
#if _GLIBCXX_USE_C99
#undef snprintf
#undef vfscanf
#undef vscanf
#undef vsnprintf
#undef vsscanf
_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
#if _GLIBCXX_USE_C99_CHECK || _GLIBCXX_USE_C99_DYNAMIC
extern "C" int
(snprintf)(char * __restrict, std::size_t, const char * __restrict, ...)
throw ()
extern "C" int
(vfscanf)(FILE * __restrict, const char * __restrict, __gnuc_va_list)
extern "C" int (vscanf)(const char * __restrict, __gnuc_va_list)
extern "C" int
(vsnprintf)(char * __restrict, std::size_t, const char * __restrict,
__gnuc_va_list) throw ()
extern "C" int
(vsscanf)(const char * __restrict, const char * __restrict, __gnuc_va_list)
throw ()
#endif
#if !_GLIBCXX_USE_C99_DYNAMIC
using ::snprintf
using ::vfscanf
using ::vscanf
using ::vsnprintf
using ::vsscanf
#endif
_GLIBCXX_END_NAMESPACE
_GLIBCXX_BEGIN_NAMESPACE(std)
using ::__gnu_cxx::snprintf
using ::__gnu_cxx::vfscanf
using ::__gnu_cxx::vscanf
using ::__gnu_cxx::vsnprintf
using ::__gnu_cxx::vsscanf
_GLIBCXX_END_NAMESPACE
#endif // _GLIBCXX_USE_C99
#endif
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)