免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: fibbery
打印 上一主题 下一主题

我需要一个使用stl库中的异常类的例子,谢谢! [复制链接]

论坛徽章:
0
11 [报告]
发表于 2008-08-11 16:40 |只看该作者
原帖由 fibbery 于 2008-8-11 16:35 发表
to tyc661:

我确实不知道C++标准程序库不等于STL,C++标准程序库指的是什么?STL范围是什么?多谢,多谢!

差不多你可以认为:C++标准库 = STL + “从C继承过来的库”

论坛徽章:
0
12 [报告]
发表于 2008-08-11 16:41 |只看该作者
继承你想要继承的那个异常
重写what();
在异常的构造时 将你要打印的信息传入

论坛徽章:
0
13 [报告]
发表于 2008-08-11 16:45 |只看该作者
3.3.3 Throwing Standard Exceptions
You can throw standard exceptions inside your own library or program. All standard exception
classes that enable you to do this have only one parameter to create the exception: a string
(class string is described in Chapter 11) that will become the description returned by
what(). For example, the class logic_error is defined as follows:
namespace std {
class logic_error : public exception {
public:
explicit logic_error (const string& whatString);
};
}
The set of standard exceptions that provide this ability contains class logic_error and its
derived classes, class runtime_error and its derived classes, as well as class
ios_base::failure. Thus, you can't throw exceptions of the base class exception and any
exception class that is provided for language support.
To throw a standard exception, you simply create a string that describes the exception and use it
to initialize the thrown exception object:
string s;
...
throw out_of_range(s);
Implicit conversions from char* to string exist, so you can also use a string literal directly:
throw out_of_range("out_of_range exception (somewhere, somehow)";

3.3.4 Deriving Standard Exception ClassesAnother possibility for using the standard exception classes in your code is to define a special
exception class derived directly or indirectly from class exception. To do this, you must ensure
that the what() mechanism works.
The C++ Standard Library
dyne-book 34
The member function what() is virtual. So, one way to provide what() is to write your own
implementation of what():
namespace MyLib {
/* user-defined exception class
* derived from a standard class for exceptions
*/
class MyProblem : public std::exception {
public:
...
MyProblem(...) { //special constructor
}
virtual const char* what() const throw() {
//what() function
...
}
};
...
void f() {
...
//create an exception object and throw it
throw MyProblem(...);
...
}
}
Another way to provide the what() function is to derive your exception class from one of the
classes that have a string constructor for the what() argument:
namespace MyLib {
/* user-defined exception class
* - derived from a standard class for exceptions
* that has a constructor for the what() argument
*/
class MyRangeProblem : public std:ut_of_range {
public:
MyRangeProblem (const string& whatString)
: out_of_range(whatString) {
}
};
...
void f() {
...
//create an exception object by using a string constructor
and throw it
throw MyRangeProblem("here is my special range problem";
...
}
}
For examples that are part of a complete program, see class Stack on page 441 and class
Queue on page 450.

[ 本帖最后由 fibbery 于 2008-8-11 16:53 编辑 ]

论坛徽章:
0
14 [报告]
发表于 2008-08-11 16:46 |只看该作者
可以直接用标准库中exception的几个派生类,在头文件<stdexcept>中:
domain_error、invalid_argument、length_error、out_of_range、overflow_error、range_error、underflow_error

还可以自己从中派生异常类,可以在其中加入错误码等其它信息

论坛徽章:
0
15 [报告]
发表于 2008-08-11 16:49 |只看该作者
原帖由 tyc611 于 2008-8-11 16:40 发表

差不多你可以认为:C++标准库 = STL + “从C继承过来的库”


哦,STL是标准模板库
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP