热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

c++exceptionshortnote

~Hereisasimplenoteforc++exceptionusage.#include#include#include

《c++ exception short note》 ~

Here is a simple note for c++ exception usage.

#include
#include
#include
class CBad : public std::exception
{
public:
const char* what() const throw () { //`const throw()` means it won't throw any exception.
return "CBad";
}
};
class BBad : public std::exception
{
public:
const char* what() const throw () {
return "BBad";
}
};
class C{
public:
C(int x) throw(CBad){
printf("C(%d)...\n", x);
if (x <0) {
//error
throw CBad();
}
}
~C(){
printf("~C()...\n");
}
};
class B{
public:
B(int x) throw(BBad){
printf("B(%d)...\n", x);
if (x <0) {
throw BBad();
}
}
~B(){
printf("~B()...\n");
}
};
class A{
public:
A(int b, int c)
try: b_(new B(b)),
c_(new C(c))
{
printf("A()...\n");
}
catch (BBad & e) {
std::cout <<"b fucked" < //Q: will "new C(c)" run ? A: won't. so here we don't need to do other things.
throw;
}
catch (CBad & e) { //if no exception introduced, the resources held by B is not going to be released.
std::cout <<"c fucked, but b is ok. so we have to release b's resources." < delete b_;
throw;
}
catch (...) {
throw;
}
~A() {
printf("~A()...\n");
delete b_;
delete c_;
}
private:
B *b_;
C *c_;
};
void custom_termination()
{
abort();
}
int main()
{
try {
A(1, -2);
} catch (...) {
printf("other exception\n");
//if A's construtor generate any exception, we can:
// 1. throw it to higher level ( here the exception will go calling `std::terminate()`. )
// throw;
// 2. continue do other things if this error can be tolarated.
// do nothing;
// 3. custom termination functions.
// ourterminate();
custom_termination();
}
return 0;
}

a further example:

#include
#include
#include
#include
class XBad : public std::exception
{
public:
explicit XBad(const char *msg) throw()
: msg_(msg) {
}
virtual const char* what() const throw () {
return msg_;
}
protected:
const char* msg_;
};
class CBad : public XBad
{
public:
explicit CBad(const char *msg) throw()
: XBad(msg) {
}
explicit CBad(int ec) throw()
: XBad(strerror(ec))
{
}
};
class BBad : public XBad
{
public:
explicit BBad(const char *msg) throw()
: XBad(msg) {
}
};
class C{
public:
C(int x) throw(CBad){
printf("C(%d)...\n", x);
if (x <0) {
//error
throw CBad(x);
}
}
~C(){
printf("~C()...\n");
}
};
class B{
public:
B(int x) throw(BBad){
printf("B(%d)...\n", x);
if (x <0) {
throw BBad(".... is not permit.");
}
}
~B(){
printf("~B()...\n");
}
};
class A{
public:
A(int b, int c)
try: b_(new B(b)),
c_(new C(c))
{
printf("A()...\n");
}
catch (const BBad & e) {
std::cout <<"b fucked" < //Q: will "new C(c)" run ? A: won't. so here we don't need to do other things.
throw e;
}
catch (const CBad & e) { //if no exception introduced, the resources held by B is not going to be released.
std::cout <<"c fucked, but b is ok. so we have to release b's resources." < delete b_;
throw e;
}
catch (...) {
throw XBad("other exception");
}
~A() {
printf("~A()...\n");
delete b_;
delete c_;
}
private:
B *b_;
C *c_;
};
void custom_termination()
{
abort();
}
int main()
{
try {
A(1, -2);
} catch ( const XBad & x ){
printf("Error: %s\n", x.what());
abort();
//if A's construtor generate any exception, we can:
// 1. throw it to higher level ( here the exception will go calling `std::terminate()`. )
// throw;
// 2. continue do other things if this error can be tolarated.
// do nothing;
// 3. custom termination functions.
// ourterminate();
} catch (...) {
printf("other exception\n");
custom_termination();
}
return 0;
}

推荐阅读
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文介绍了一种划分和计数油田地块的方法。根据给定的条件,通过遍历和DFS算法,将符合条件的地块标记为不符合条件的地块,并进行计数。同时,还介绍了如何判断点是否在给定范围内的方法。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 电话号码的字母组合解题思路和代码示例
    本文介绍了力扣题目《电话号码的字母组合》的解题思路和代码示例。通过使用哈希表和递归求解的方法,可以将给定的电话号码转换为对应的字母组合。详细的解题思路和代码示例可以帮助读者更好地理解和实现该题目。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 本文介绍了解决二叉树层序创建问题的方法。通过使用队列结构体和二叉树结构体,实现了入队和出队操作,并提供了判断队列是否为空的函数。详细介绍了解决该问题的步骤和流程。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • Linux环境变量函数getenv、putenv、setenv和unsetenv详解
    本文详细解释了Linux中的环境变量函数getenv、putenv、setenv和unsetenv的用法和功能。通过使用这些函数,可以获取、设置和删除环境变量的值。同时给出了相应的函数原型、参数说明和返回值。通过示例代码演示了如何使用getenv函数获取环境变量的值,并打印出来。 ... [详细]
  • 本文介绍了PE文件结构中的导出表的解析方法,包括获取区段头表、遍历查找所在的区段等步骤。通过该方法可以准确地解析PE文件中的导出表信息。 ... [详细]
  • 本文介绍了一个题目的解法,通过二分答案来解决问题,但困难在于如何进行检查。文章提供了一种逃逸方式,通过移动最慢的宿管来锁门时跑到更居中的位置,从而使所有合格的寝室都居中。文章还提到可以分开判断两边的情况,并使用前缀和的方式来求出在任意时刻能够到达宿管即将锁门的寝室的人数。最后,文章提到可以改成O(n)的直接枚举来解决问题。 ... [详细]
author-avatar
qwer
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有