当前位置: 首页 > news >正文

一流的微商城网站建设网站建设的会计分录

一流的微商城网站建设,网站建设的会计分录,平台公司拿地,wordpress 转跳到登录1.C 异常概念 异常是一种处理错误的方式,当一个函数发现自己无法处理的错误时就可以抛出异常,让函数的直接或间接的调用者处理这个错误。 throw:当问题出现时,程序会抛出一个异常。这是通过使用 throw 关键字来完成的。catch&am…

1.C++ 异常概念

异常是一种处理错误的方式,当一个函数发现自己无法处理的错误时就可以抛出异常,让函数的直接或间接的调用者处理这个错误。

  • throw:当问题出现时,程序会抛出一个异常。这是通过使用 throw 关键字来完成的。
  • catch:在您想要处理问题的地方,通过异常处理程序捕获异常。catch 关键字用于捕获异常,可以有多个 catch 进行捕获。
  • trytry 块中的代码标识将被激活的特定异常,它后面通常跟着一个或多个 catch 块。

如果有一个块抛出一个异常,捕获异常的方法会使用 try 和 catch 关键字。try 块中放置可能抛出异常的代码,try 块中的代码被称为保护代码。

2. 异常的抛出和捕获

异常的抛出和匹配原则
  1. 异常是通过抛出对象而引发的,该对象的类型决定了应该激活哪个 catch 的处理代码。
  2. 被选中的处理代码是调用链中与该对象类型匹配且离抛出异常位置最近的那一个。
  3. 抛出异常对象后,会生成一个异常对象的拷贝,因为抛出的异常对象可能是一个临时对象,所以会生成一个拷贝对象,这个拷贝的临时对象会在被 catch 以后销毁。(这里的处理类似于函数的传值返回)
  4. catch(...) 可以捕获任意类型的异常,问题是不知道异常错误是什么。
  5. 实际中抛出和捕获的匹配原则有个例外,并不都是类型完全匹配,可以抛出的派生类对象,使用基类捕获,这个在实际中非常实用,我们后面会详细讲解这个。
在函数调用链中异常栈展开匹配原则
  1. 首先检查 throw 本身是否在 try 块内部,如果是再查找匹配的 catch 语句。如果有匹配的,则调到 catch 的地方进行处理。
  2. 没有匹配的 catch 则退出当前函数栈,继续在调用函数的栈中进行查找匹配的 catch
  3. 如果到达 main 函数的栈,依旧没有匹配的,则终止程序。上述这个沿着调用链查找匹配的 catch 子句的过程称为栈展开。所以实际中我们最后都要加一个 catch(...) 捕获任意类型的异常,否则当有异常没捕获,程序就会直接终止。
  4. 找到匹配的 catch 子句并处理以后,会继续沿着 catch 子句后面继续执行。

代码展示

void exe()
{throw 1;
}
void func()
{int x = 2, y = 0;auto ret = [=]()->int {if (y == 0) { throw " 除0错误"; } else { return x / y; }};std::cout << ret() << std::endl;
}
int main()
{try{func();exe();}catch (const char*tsr){cerr <<tsr << endl;}catch (...){cerr << "未知异常!" << endl;}return 0;
}

下面展示上面的代码进阶版也就是,当你捕获到异常进行紧急修复,然后重新抛出!重新抛出的异常会被调用链上层捕捉!切记这个调用链是给上走的!

void exe() {throw 1;
}void func() {int x = 2, y = 0;auto ret = [=]()->int {if (y == 0) {// 抛出标准异常类型,便于统一处理throw std::runtime_error("除0错误");} else {return x / y;}};try {std::cout << ret() << std::endl;} catch (const std::runtime_error& e) {std::cerr << e.what() << std::endl;// 修复除零错误,将除数修改为非零值y = 1;auto newRet = [x, y]()->int { return x / y; };std::cout << "修复后结果: " << newRet() << std::endl;// 重新抛出异常throw;}
}int main() {try {func();exe();} catch (const std::runtime_error& e) {std::cerr << "main函数捕获到重新抛出的异常: " << e.what() << std::endl;std::cout << "异常已在下层函数修复,程序继续执行" << std::endl;} catch (int e) {std::cerr << "捕获到异常: " << e << std::endl;// 这里可以添加对异常值为 1 的具体处理逻辑std::cout << "异常已处理,程序继续执行" << std::endl;} catch (...) {std::cerr << "未知异常!" << std::endl;}return 0;
}    

3.继承抛异常


//
//在实际当中有一个抛出和捕获特例而且也非常常用!可以抛出派生类对象,让基类去捕获
#include <iostream>
#include <thread>
#include <chrono>
#include <cstdlib>
#include <ctime>
#include <string>// 假设存在基类 Exception,这里未给出完整定义,实际需包含相关头文件或完整实现
void SQLMgr();
//公司常用的异常继承体系!
class Exception 
{
public:Exception(const std::string& errmsg, int id) : _errmsg(errmsg), _id(id) {}virtual std::string what() const = 0;
protected:std::string _errmsg;int _id;
};class HttpServerException : public Exception {
public:HttpServerException(const std::string& errmsg, int id, const std::string& type): Exception(errmsg, id), _type(type) {}virtual std::string what() const override {std::string str = "HttpServerException:";str += _type;str += ":";str += _errmsg;return str;}
private:const std::string _type;
};class CacheException : public Exception {
public:CacheException(const std::string& errmsg, int id): Exception(errmsg, id) {}virtual std::string what() const override {std::string str = "CacheException:";str += _errmsg;return str;}
};class SqlException : public Exception {
public:SqlException(const std::string& errmsg, int id, const std::string& sql): Exception(errmsg, id), _sql(sql) {}virtual std::string what() const override {std::string str = "SqlException:";str += _errmsg;str += "->";str += _sql;return str;}
private:const std::string _sql;
};void CacheMgr() {srand(time(0));if (rand() % 5 == 0) {throw CacheException("权限不足", 100);}else if (rand() % 6 == 0) {throw CacheException("数据不存在", 101);}SQLMgr();
}void HttpServer() {srand(time(0));if (rand() % 3 == 0) {throw HttpServerException("请求资源不存在", 100, "get");}else if (rand() % 4 == 0) {throw HttpServerException("权限不足", 101, "post");}CacheMgr();
}void SQLMgr() {srand(time(0));if (rand() % 7 == 0) {throw SqlException("权限不足", 100, "select * from name = '张三'");}//throw "xxxxxx";
}int main() {while (1) {std::this_thread::sleep_for(std::chrono::seconds(1));try {HttpServer();}catch (const Exception& e) //捕获父类对象{std::cout << e.what() << std::endl;}catch (...) {std::cout << "Unkown Exception" << std::endl;}}return 0;
}

4.异常安全

5.库里面的异常:

http://www.dtcms.com/a/560254.html

相关文章:

  • 网站临时会话公司门户网站
  • 建设单位网站cms是什么公司简称
  • 做网站东莞长沙网站建设策划
  • 足球外围网站怎么做做进口产品的网站好
  • 那些网站可以接私活做无锡网站制作公司
  • 哪有网站建设的wordpress手机端模板
  • 丰台建站公司做销售网站的好做么
  • 秒收的网站用vs做网站如何连接数据库
  • 全国网站建设人员数量wordpress review主题
  • 厦门微网站开发wordpress 同步 微信
  • 网站备案需要具备什么条件现在做网站还有用吗
  • 国外互联网裁员长春seo排名
  • 设计网站需要的知识安卓app做网站外壳
  • 坑梓网站建设如何深圳营销型网站建设制作商
  • 优酷视频网站源码口碑seo推广公司
  • 外贸网站建设行业发展企业微信app官网下载
  • 滑动网站wordpress aliyun
  • 淘宝怎么发布网站建设网页制作的公司排名
  • 西安网站建设兼职阳春做网站公司
  • 查网站服务器速度南昌网站排名
  • 提供微网站建设中国建设银行官网站住房公积金
  • 网站备案查询背景布asia域名
  • 烟台网站制作专业电商平台推广方式有哪些
  • 做产地证的网站服装设计公司取名
  • 福州网站定制设计psdw做网站
  • 网站建设宣传册内容龙岩网站建设全包
  • 宜春seo网站推广后台网站手机版视频怎么做
  • 怎么做网站推广世界杯织梦建设网站全过程
  • 郑州网站建设的公司计算机网页制作工具
  • 湖州专业做网站wordpress更新慢