JavaScript is required to for searching.
跳过导航链接
退出打印视图
Oracle Solaris Studio 12.3:C++ 用户指南     Oracle Solaris Studio 12.3 Information Library (简体中文)
search filter icon
search icon

文档信息

前言

第 1 部分C++ 编译器

1.  C++ 编译器

2.  使用 C++ 编译器

3.  使用 C++ 编译器选项

第 2 部分编写 C++ 程序

4.  语言扩展

5.  程序组织

6.  创建和使用模板

7.  编译模板

8.  异常处理

8.1 同步和异步异常

8.2 指定运行时错误

8.3 禁用异常

8.4 使用运行时函数和预定义的异常

8.5 将异常与信号和 Setjmp/Longjmp 混合使用

8.6 生成具有异常的共享库

9.  改善程序性能

10.  生成多线程程序

第 3 部分库

11.  使用库

12.  使用 C++ 标准库

13.  使用传统 iostream

14.  生成库

第 4 部分附录

A.  C++ 编译器选项

B.  Pragma

词汇表

索引

8.4 使用运行时函数和预定义的异常

标准头文件 <exception> 提供了 C++ 标准中指定的类和异常相关函数。仅在标准模式(编译器缺省模式,或使用选项 -compat=5)下编译时才可访问该头文件。以下摘录的部分代码显示了 <exception> 头文件声明。

// standard header <exception>
namespace std {
    class exception {
           exception() throw();
           exception(const exception&) throw();
           exception& operator=(const exception&) throw();
           virtual ~exception() throw();
           virtual const char* what() const throw();
    };
    class bad_exception: public exception {...};
    // Unexpected exception handling
       typedef void (*unexpected_handler)();
       unexpected_handler
         set_unexpected(unexpected_handler) throw();
       void unexpected();
    // Termination handling
       typedef void (*terminate_handler)();
       terminate_handler set_terminate(terminate_handler) throw();
       void terminate();
       bool uncaught_exception() throw();
}

标准类 exception 是所选语言构造或 C++ 标准库抛出的所有异常的基类。可以构造、复制及销毁类型为 exception 的对象,而不会生成异常。虚拟成员函数 what() 返回描述异常的字符串。

为了与 C++ 4.2 版中所用的异常兼容,还提供了头文件 <exception.h> 以用于标准模式下。该头文件允许转换到标准 C++ 代码,并包含了不是标准 C++ 部分的声明。应在开发进度计划许可的情况下,更新代码以遵循 C++ 标准(使用 <exception> 而非 <exception.h>)。

// header <exception.h>, used for transition
#include <exception>
#include <new>
using std::exception;
using std::bad_exception;
using std::set_unexpected;
using std::unexpected;
using std::set_terminate;
using std::terminate;
typedef std::exception xmsg;
typedef std::bad_exception xunexpected;
typedef std::bad_alloc xalloc;