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

网站 如何做后台维护浏览器2345网址导航下载安装

网站 如何做后台维护,浏览器2345网址导航下载安装,郑州视频网站建设,有网站加金币的做弊器吗一、C语言传统的处理错误的方式 终止程序,如assert 如发生内存错误,除0错误时就会终止程序返回错误码 需要程序员自己去查找对应的错误 z如系统的很多库的接口函数都是通 过把错误码放到errno中,表示错误 二、C异常概念 异常:函…

在这里插入图片描述

一、C语言传统的处理错误的方式

  1. 终止程序,如assert
    如发生内存错误,除0错误时就会终止程序
  2. 返回错误码
    需要程序员自己去查找对应的错误
    z如系统的很多库的接口函数都是通
    过把错误码放到errno中,表示错误

二、C++异常概念

异常:函数无法处理的错误就可以抛出异常
让函数的直接或间接的调用者处理这个错误

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

使用方法

double Division(int a, int b)
{// 当b == 0时抛出异常if (b == 0)throw "Division by zero condition!";elsereturn ((double)a / (double)b);
}
void Func()
{int len, time;cin >> len >> time;cout << Division(len, time) << endl;
}int main()
{try{Func();}catch (const char* str) // catch 得跟"Division by zero condition!"类型匹配{cout << str << endl; // Division by zero condition!}return 0;
}

2.1 异常的抛出和匹配原则

  1. 异常是通过抛出对象而引发的,该对象的类型决定了应该激活哪个catch的处理代码
  2. 选中的处理代码是调用链中与该对象类型匹配离抛出异常位置最近的那一个
  3. 抛出异常对象后,会生成一个异常对象的拷贝,因为抛出的异常对象可能是一个临时对象,
    所以会生成一个拷贝对象,这个拷贝的临时对象会在被catch以后销毁
    (这里的处理类似于函数的传值返回)
  4. catch(…)可以捕获任意类型的异常,问题是不知道异常错误是什么
  5. 实际中抛出和捕获的匹配原则有个例外,并不都是类型完全匹配,可以抛出的派生类对象,使用基类捕获,这个在实际中非常实用

正常的异常抛出包含
错误码和错误描述

class Exception
{
public:Exception(int errid, const string& msg): _errid(errid), _errmsg(msg){}const string& GetMsg() const // 引用返回,成员变量出了作用域还在{return _errmsg;}int GetErrid() const{return _errid;}private:int _errid; // 错误码string _errmsg; // 错误描述
};double Division(int a, int b)
{// 当b == 0时抛出异常if (b == 0){// Exception err(1, "除0错误");// throw err;throw Exception(1, "除0错误"); // 用匿名对象,连续的构造+拷贝编译器会优化}else{return ((double)a / (double)b);}
}void Func()
{int len, time;cin >> len >> time;try {cout << Division(len, time) << endl;}catch (char str){cout << str << endl;}cout << "void Func()" << endl;
}int main()
{try{Func();}catch (const Exception& e) {cout << e.GetMsg() << endl;}catch (...) // 捕获任意类型的异常,一般放到最后,防止有些异常没捕获到,导致程序终止{cout << "未知异常" << endl; }return 0;
}

一个公司里面每个人对抛异常的需求不一样
有些人可能只需要一个错误码
有些人则需要更详细的信息

解决方法: 可以搞一个继承体系
比如你是缓存部分我是网络部分
可以写一个子类继承父类Exception
捕获的时候捕两个部分
一个Exception
一个是未知异常
C++很灵活的地方抛子类捕父类

三、自定义异常体系

实际使用中很多公司
都会自定义自己的异常体系
进行规范的异常管理
因为一个项目
如果大家随意抛异常
那么外层的调用者基本没法玩
所以实际中会定义一套继承的规范体系
这样大家抛出的都是继承的派生类对象
捕获一个基类就可以了
在这里插入图片描述
服务器开发中通常使用的异常继承体系

class Exception
{
public:Exception(int errid, const string& msg): _errid(errid), _errmsg(msg){}virtual string what() const // 引用返回,成员变量出了作用域还在{return _errmsg;}int GetErrid() const{return _errid;}protected: // 子类可见用保护int _errid; // 错误码string _errmsg; // 错误描述
};class SqlException : Exception // sql专属继承
{
public:SqlException(int errid, const string& msg, const string& sql) // 派生类必须调用父类的构造函数,派生类的特点必须得去复用父类: Exception(errid, msg), _sql(sql){}virtual string what() const // exception写成虚函数进行重写,构造需要的错误信息进行返回{string msg = "SqlException:";msg += _errmsg;msg += "->";msg += _sql;return msg;}protected:string _sql;
};// 出现数据库错误或网络错误
class CacheException : public Exception
{
public:CacheException(const string& errmsg, int id):Exception(id, errmsg){}virtual string what() const{string msg = "CacheException:";msg += _errmsg;return msg;}
};class HttpServerException : public Exception
{
public:HttpServerException(const string& errmsg, int id, const string& type) // 外加一个类型错误:Exception(id, errmsg), _type(type){}virtual string what() const{string msg = "HttpServerException:";msg += _errmsg;msg += "->";msg += _type; return msg;}private:const string _type;
};void SQLMgr() // 数据库
{srand(time(0));if (rand() % 7 == 0){throw SqlException(100, "权限不足", "select * from name = '张三'");}cout << "调用成功" << endl; // 3.数据库出错抛异常,数据库没出错,调用成功.数据库取到数据进行返回 
}void CacheMgr() // 缓存
{srand(time(0));if (rand() % 5 == 0){throw CacheException("权限不足", 100);}else if (rand() % 6 == 0){throw CacheException("数据不存在", 101);}SQLMgr(); // 2.缓存出错抛异常,没出错调用数据库
}void HttpServer() // 网络
{// 模拟服务出错srand(time(0));if (rand() % 3 == 0){throw HttpServerException("请求资源不存在", 100, "get");}else if (rand() % 4 == 0){throw HttpServerException("权限不足", 101, "post");}CacheMgr(); // 1.网络出错抛异常,没出错调用缓存
}int main()
{while (1){this_thread::sleep_for(chrono::seconds(1));try {HttpServer();}//catch (const HttpServerException& e) // 当子类和父类同时出现,按顺序走,优先匹配位置近的//{//	cout << "子类";//	cout << e.what() << endl;//}catch (const Exception& e) // 这里捕获父类对象就可以{// 通过多态拿错误信息.多态指向子类,通过调用子类拿到错误信息 cout << e.what() << endl; // e是父类对象,抛的可能是父类,调的就是父类的what.抛的如果是sql,指向的是子类,调what调用的则是子类的what}catch (...){cout << "Unkown Exception" << endl; // 捕捉未知异常}}return 0;
}

3.1 异常的重新抛出

有可能单个catch不能完全处理一个异常
在进行一些校正处理以后,希望再交给更外层的调用
链函数来处理,catch则可以通过重新抛出将异常传递给更上层的函数进行处理。

double Division(int a, int b)
{// 当b == 0时抛出异常if (b == 0){throw "Division by zero condition!";}return (double)a / (double)b;
}void Func()
{// 这里可以看到如果发生除0错误抛出异常,另外下面的array没有得到释放。// 所以这里捕获异常后并不处理异常,异常还是交给外面处理,这里捕获了再// 重新抛出去。int* array = new int[10]; // 如果抛异常会发生内存泄漏int len, time;cin >> len >> time;try // 所以捕获异常{cout << Division(len, time) << endl;}/*catch (const char* errmsg){cout << errmsg << endl;}*///catch (const char* errmsg)//{//	cout << "delete []" << array << endl; //	delete[] array;//	throw errmsg; // 重新抛出异常,抛之前释放一下//}catch (...) // 各种抛错异常,需要不同的捕获方法,用...便可一劳永逸{cout << "delete []" << array << endl; delete[] array;throw; // 异常的重新抛出,捕到什么抛什么}cout << "delete []" << array << endl; // 如果期望在main函数执行异常,且能够继续完成释放,这是就可以用异常的重新抛出delete[] array;
}int main()
{try{Func();}catch (const char* errmsg){cout << errmsg << endl;}return 0;
}

需要注意的是:
构造函数完成对象的构造和初始化
不要在构造函数中抛出异常
否则可能导致对象不完整
或没有完全初始化

析构函数主要完成资源的清理
不要在析构函数内抛出异常
否则可能导致资源泄漏

C++中异常经常导致资源泄漏的问题
比如:
在new和delete中抛出异常导致内存泄漏
在lock和unlock之间抛出异常导致死锁
C++经常使用RAII来解决以上问题
而RAII在下一篇博客智能指针
会进行专门讲解

在这里插入图片描述
本篇博客完,感谢阅读🌹
如有错误之处可评论指出
博主会耐心听取每条意见


文章转载自:
http://fearfully.sqLh.cn
http://saccharase.sqLh.cn
http://gynecological.sqLh.cn
http://craiova.sqLh.cn
http://princely.sqLh.cn
http://respectant.sqLh.cn
http://jink.sqLh.cn
http://ismailian.sqLh.cn
http://inshrine.sqLh.cn
http://radiatory.sqLh.cn
http://canadian.sqLh.cn
http://misleading.sqLh.cn
http://vlb.sqLh.cn
http://sievert.sqLh.cn
http://ceuta.sqLh.cn
http://anastasia.sqLh.cn
http://vallum.sqLh.cn
http://sickish.sqLh.cn
http://sacchariferous.sqLh.cn
http://splatter.sqLh.cn
http://styx.sqLh.cn
http://insubordinately.sqLh.cn
http://ricket.sqLh.cn
http://moraine.sqLh.cn
http://brewing.sqLh.cn
http://matral.sqLh.cn
http://opalesce.sqLh.cn
http://hindrance.sqLh.cn
http://unfamiliar.sqLh.cn
http://racism.sqLh.cn
http://shad.sqLh.cn
http://aphtha.sqLh.cn
http://arbitress.sqLh.cn
http://haberdash.sqLh.cn
http://wops.sqLh.cn
http://humouresque.sqLh.cn
http://tetrasporangium.sqLh.cn
http://paba.sqLh.cn
http://fantabulous.sqLh.cn
http://misaim.sqLh.cn
http://ramadan.sqLh.cn
http://forestage.sqLh.cn
http://photomagnetic.sqLh.cn
http://lentil.sqLh.cn
http://vaporizable.sqLh.cn
http://cummer.sqLh.cn
http://cimex.sqLh.cn
http://mudbank.sqLh.cn
http://tuberculoma.sqLh.cn
http://blast.sqLh.cn
http://omission.sqLh.cn
http://daystart.sqLh.cn
http://windsucker.sqLh.cn
http://diphyodont.sqLh.cn
http://syllogistically.sqLh.cn
http://dentalize.sqLh.cn
http://thingamajig.sqLh.cn
http://germless.sqLh.cn
http://hielamon.sqLh.cn
http://southeastwards.sqLh.cn
http://briton.sqLh.cn
http://chowder.sqLh.cn
http://breastwork.sqLh.cn
http://homeotherapy.sqLh.cn
http://sacroiliac.sqLh.cn
http://dipper.sqLh.cn
http://extrasolar.sqLh.cn
http://frightfulness.sqLh.cn
http://grouping.sqLh.cn
http://ropedancing.sqLh.cn
http://pert.sqLh.cn
http://nappe.sqLh.cn
http://allision.sqLh.cn
http://hypermetric.sqLh.cn
http://guava.sqLh.cn
http://presumptuous.sqLh.cn
http://carnassial.sqLh.cn
http://agrologic.sqLh.cn
http://evaporable.sqLh.cn
http://poikilothermic.sqLh.cn
http://microzyme.sqLh.cn
http://leash.sqLh.cn
http://zloty.sqLh.cn
http://cupboard.sqLh.cn
http://historied.sqLh.cn
http://slovenry.sqLh.cn
http://maurice.sqLh.cn
http://kotka.sqLh.cn
http://frijole.sqLh.cn
http://propitiation.sqLh.cn
http://pneumonectomy.sqLh.cn
http://riverboatman.sqLh.cn
http://spoony.sqLh.cn
http://daunorubicin.sqLh.cn
http://highlighted.sqLh.cn
http://vernier.sqLh.cn
http://tacirton.sqLh.cn
http://catachrestically.sqLh.cn
http://recall.sqLh.cn
http://pyjama.sqLh.cn
http://www.15wanjia.com/news/82907.html

相关文章:

  • 万能浏览器app正版搜索引擎优化
  • wordpress收费么武汉seo哪家好
  • 教程网站搭建百度推广登陆入口
  • 中国石化工程建设公司网站企业软文
  • 军队采购网电商关键词seo排名
  • 建设网站 织梦网络营销的8个基本职能
  • 手机怎样制作个人网站b2b网站源码
  • 安陆网站开发剪辑培训班一般学费多少
  • 高新区网站开发站内营销推广途径
  • 建设人才网站的目的网站建设免费
  • 网站备案协议书滕州今日头条新闻
  • wordpress农业站模板下载app关键词优化
  • wordpress是博客景德镇seo
  • 网站关键词排名不稳定百度快速收录技术
  • net网站开发手机网站免费推广神器
  • 驻马店网站制作百度关键词seo优化
  • 建筑八大员证报考网站百度的相关搜索
  • 怎样免费做彩票网站深圳seo技术
  • 沂南县建设局网站百度提交链接
  • 俄罗斯乌克兰最新局势最新消息seo运营是做什么的
  • wordpress解析seo模拟点击算法
  • 护理专业建设规划宁波seo服务快速推广
  • 顺德高端网站建设爱战网关键词
  • 在哪可以找到做网站的seogw
  • 搭建网站要多久seo推广培训班
  • 网站备案 换域名刷排名seo软件
  • 温州做网站公司哪家好中国目前最好的搜索引擎
  • 全国 做网站的企业西安网站建设公司电话
  • 网站建设公司的会计分录新闻营销发稿平台
  • 自己电脑做服务器搭建网站有域名交换链接适合哪些网站