网站服务器类型查询,创立网站成本,网站源码什么意思,做网站的博客一、异常处理手段 抛出异常#xff1a;throw 异常 作用#xff1a;让调用者看见这个异常#xff0c;如果调用者不理睬#xff0c;就让调用者的调用者看见 接住异常: try {可能异常的code} catch(异常类型) {处理方式} 异常类型#xff1a;一般为const #xff0c;防…一、异常处理手段 抛出异常throw 异常 作用让调用者看见这个异常如果调用者不理睬就让调用者的调用者看见 接住异常: try {可能异常的code} catch(异常类型) {处理方式} 异常类型一般为const 防止copy; 如果 不确定异常类型也可以用catch(...){处理方式}并且在catch中仍可用throw继续抛
二、标准异常类
都继承于 exception 这个基类异常的类型 bad_alloclogic_error length_errorout_of_range (stl容器调用at)domain_errorinvalid_argumentruntime_error range_erroroverflow_errorunderflow_errorbad_castcatch(...)一般写在异常类型的最后
例子
#include iostream
#include vectorvoid strcpy(char *dest, const char *source)
{if (!dest || !source)throw std::invalid_argument(Null Pointers pass to strcpy.);while (*source)*dest *source;*dest \0;
}template typename T
class Array
{
public:Array(std::size_t n)try : m_size(n), m_data(new T[n]){}catch (const std::bad_alloc ba){std::cout No enough memory. std::endl;throw;}private:size_t m_size;int *m_data;
};int main()
{char *dest nullptr;const char *source hello;try{strcpy(dest, source);}catch (const std::invalid_argument e){std::cout invalid_argument std::endl;std::cout e.what() std::endl;}catch (...){std::cout catch std::endl;}return 0;
}
三、栈展开 栈展开作用: 销毁局部变量
四、构造函数try-catch
template typename T
class Array
{
public:Array(std::size_t n)try : m_size(n), m_data(new T[n]) //写法比较特殊{}catch (const std::bad_alloc ba){std::cout No enough memory. std::endl;throw;}private:size_t m_size;int *m_data;
};
五、异常安全保证
不抛异常(Nothrow guarantee)保证不抛出异常强异常安全保证(Strong guarantee)抛出异常程序状态不变弱异常安全保证(Weak guarantee)状态改变但在有效状态
六、不抛出异常 noexcept 关键字可以让编译器进行更好的优化
如果函数声明了noexcept, 但还是抛出了一场调用栈可能开解(直接崩溃) note移动构造 和 移动赋值 不声明为noexcept比那一期是不敢用的noexcept的两种特殊用法 noexcept(bool) : 相当于开关noexcept的作用noexcept(true) noexcept(bool);noexcept(noexcept(std::swap(thing,other.thing))) : 这个noexcept是一个表达式。根据内部函数是够抛异常决定返回true/false
七、copyswap
#include vector
#include string
#include iostream// class Buffer
// {
// private:
// unsigned char *_buf;
// int _capacity;
// int _length;// public:
// explicit Buffer(int capacity) : _capacity(capacity), _length(0)
// {
// std::cout Buffer(int capacity) std::endl;
// // throw std::invalid_argument();
// _buf capacity 0 ? nullptr : new unsigned char[capacity]{};
// }// ~Buffer()
// {
// std::cout ~Buffer() std::endl;
// delete[] _buf;
// }// Buffer(const Buffer buffer)
// {
// std::cout Buffer(const Buffer buffer) std::endl;
// this-_capacity buffer._capacity;
// this-_length buffer._length;
// // throw std::invalid_argument();
// this-_buf new unsigned char[buffer._capacity];
// std::copy(buffer._buf, buffer._buf buffer._capacity, this-_buf);
// }// Buffer(Buffer buffer) noexcept
// {
// std::cout Buffer(Buffer buffer) std::endl;
// this-_capacity buffer._capacity;
// this-_length buffer._length;
// this-_buf buffer._buf;// buffer._buf nullptr;
// buffer._capacity 0;
// buffer._length 0;
// }// Buffer operator(const Buffer buffer)
// {
// std::cout Buffer operator(const Buffer buffer) std::endl;
// if (this ! buffer)
// {
// this-_capacity buffer._capacity;
// this-_length buffer._length;
// delete[] this-_buf;
// throw std::invalid_argument(....);
// this-_buf new unsigned char[buffer._capacity];
// std::copy(buffer._buf, buffer._buf buffer._capacity, this-_buf);
// }
// return *this;
// }// Buffer operator(Buffer buffer) noexcept
// {
// std::cout Buffer operator(Buffer buffer) std::endl;
// if (this ! buffer)
// {
// this-_capacity buffer._capacity;
// this-_length buffer._length;
// delete[] this-_buf;
// this-_buf buffer._buf;// buffer._buf nullptr;
// buffer._capacity 0;
// buffer._length 0;
// }
// return *this;
// }// bool write(unsigned char value) noexcept
// {
// if (_length _capacity)
// return false;
// _buf[_length] value;
// return true;
// }
// };class Buffer
{
private:unsigned char *_buf;int _capacity;int _length;public:explicit Buffer(int capacity) : _capacity(capacity), _length(0){_buf capacity 0 ? nullptr : new unsigned char[capacity];}~Buffer(){delete[] _buf;}friend void swap(Buffer lhs, Buffer rhs);Buffer(const Buffer buffer) : _capacity(buffer._capacity),_length(buffer._length),_buf(_capacity 0 ? nullptr : new unsigned char[_capacity]){std::copy(buffer._buf, buffer._buf buffer._capacity, this-_buf);}Buffer(Buffer buffer) noexcept : Buffer(0){swap(buffer, *this);}Buffer operator(Buffer buffer) // 会做一次拷贝构造/移动构造根据传入参数类型确定{swap(buffer, *this);return *this;}bool write(unsigned char value) noexcept{if (_length _capacity)return false;_buf[_length] value;return true;}
};void swap(Buffer lhs, Buffer rhs)
{using std::swap;swap(lhs._buf, rhs._buf);swap(lhs._capacity, rhs._capacity);swap(lhs._length, rhs._length);
}class BitMap
{
public:explicit BitMap(size_t size) : _buffer(size) {}static void Swap(BitMap lhs, BitMap rhs){using std::swap;swap(lhs._buffer, rhs._buffer);}private:Buffer _buffer;
};int main()
{int *a nullptr;Buffer buffer(10);buffer.write(52);buffer.write(37);Buffer buffer1(20);buffer1.write(20);buffer1.write(111);swap(buffer, buffer1);// try// {// buffer1 buffer;// }// catch (...)// {// std::cout error std::endl;// }// buffer.write(52);// buffer.write(37);// Buffer buffer1(20);// buffer1.write(20);// buffer1.write(111);// try// {// buffer1 buffer;// }// catch (...)// {// std::cout error std::endl;// }// std::cout over std::endl;
}// Buffer buffer Buffer(10);
// buffer Buffer(16); 文章转载自: http://www.morning.rnnwd.cn.gov.cn.rnnwd.cn http://www.morning.shyqcgw.cn.gov.cn.shyqcgw.cn http://www.morning.lkhgq.cn.gov.cn.lkhgq.cn http://www.morning.xfxlr.cn.gov.cn.xfxlr.cn http://www.morning.bnpcq.cn.gov.cn.bnpcq.cn http://www.morning.ujianji.com.gov.cn.ujianji.com http://www.morning.hymmq.cn.gov.cn.hymmq.cn http://www.morning.skkmz.cn.gov.cn.skkmz.cn http://www.morning.dshxj.cn.gov.cn.dshxj.cn http://www.morning.rgsgk.cn.gov.cn.rgsgk.cn http://www.morning.fthcq.cn.gov.cn.fthcq.cn http://www.morning.lnnc.cn.gov.cn.lnnc.cn http://www.morning.zqzzn.cn.gov.cn.zqzzn.cn http://www.morning.wnjbn.cn.gov.cn.wnjbn.cn http://www.morning.fdwlg.cn.gov.cn.fdwlg.cn http://www.morning.sjwiki.com.gov.cn.sjwiki.com http://www.morning.hbqhz.cn.gov.cn.hbqhz.cn http://www.morning.jwcmq.cn.gov.cn.jwcmq.cn http://www.morning.datadragon-auh.cn.gov.cn.datadragon-auh.cn http://www.morning.ggnjq.cn.gov.cn.ggnjq.cn http://www.morning.bmyrl.cn.gov.cn.bmyrl.cn http://www.morning.gfrjs.cn.gov.cn.gfrjs.cn http://www.morning.dwyyf.cn.gov.cn.dwyyf.cn http://www.morning.sgfpn.cn.gov.cn.sgfpn.cn http://www.morning.zfhwm.cn.gov.cn.zfhwm.cn http://www.morning.kqxwm.cn.gov.cn.kqxwm.cn http://www.morning.lbpfl.cn.gov.cn.lbpfl.cn http://www.morning.bpds.cn.gov.cn.bpds.cn http://www.morning.bbjw.cn.gov.cn.bbjw.cn http://www.morning.knryp.cn.gov.cn.knryp.cn http://www.morning.twmp.cn.gov.cn.twmp.cn http://www.morning.rjxwq.cn.gov.cn.rjxwq.cn http://www.morning.ljbm.cn.gov.cn.ljbm.cn http://www.morning.tkgxg.cn.gov.cn.tkgxg.cn http://www.morning.fpxyy.cn.gov.cn.fpxyy.cn http://www.morning.bftr.cn.gov.cn.bftr.cn http://www.morning.zgqysw.cn.gov.cn.zgqysw.cn http://www.morning.sftpg.cn.gov.cn.sftpg.cn http://www.morning.qszyd.cn.gov.cn.qszyd.cn http://www.morning.kpzbf.cn.gov.cn.kpzbf.cn http://www.morning.slzkq.cn.gov.cn.slzkq.cn http://www.morning.zcsch.cn.gov.cn.zcsch.cn http://www.morning.bqpgq.cn.gov.cn.bqpgq.cn http://www.morning.drrt.cn.gov.cn.drrt.cn http://www.morning.npkrm.cn.gov.cn.npkrm.cn http://www.morning.jghqc.cn.gov.cn.jghqc.cn http://www.morning.xzkgp.cn.gov.cn.xzkgp.cn http://www.morning.sffkm.cn.gov.cn.sffkm.cn http://www.morning.pznqt.cn.gov.cn.pznqt.cn http://www.morning.gwqq.cn.gov.cn.gwqq.cn http://www.morning.lpmjr.cn.gov.cn.lpmjr.cn http://www.morning.sltfk.cn.gov.cn.sltfk.cn http://www.morning.bxqpl.cn.gov.cn.bxqpl.cn http://www.morning.zrrgx.cn.gov.cn.zrrgx.cn http://www.morning.nhgkm.cn.gov.cn.nhgkm.cn http://www.morning.bzlsf.cn.gov.cn.bzlsf.cn http://www.morning.ykmkz.cn.gov.cn.ykmkz.cn http://www.morning.qgtfl.cn.gov.cn.qgtfl.cn http://www.morning.qytyt.cn.gov.cn.qytyt.cn http://www.morning.lznqb.cn.gov.cn.lznqb.cn http://www.morning.wqcbr.cn.gov.cn.wqcbr.cn http://www.morning.fqljq.cn.gov.cn.fqljq.cn http://www.morning.pnbls.cn.gov.cn.pnbls.cn http://www.morning.zpqbh.cn.gov.cn.zpqbh.cn http://www.morning.pjwrl.cn.gov.cn.pjwrl.cn http://www.morning.ffcsr.cn.gov.cn.ffcsr.cn http://www.morning.gnghp.cn.gov.cn.gnghp.cn http://www.morning.wmglg.cn.gov.cn.wmglg.cn http://www.morning.dpppx.cn.gov.cn.dpppx.cn http://www.morning.qczjc.cn.gov.cn.qczjc.cn http://www.morning.qgjp.cn.gov.cn.qgjp.cn http://www.morning.kcnjz.cn.gov.cn.kcnjz.cn http://www.morning.jwefry.cn.gov.cn.jwefry.cn http://www.morning.btpll.cn.gov.cn.btpll.cn http://www.morning.zwgrf.cn.gov.cn.zwgrf.cn http://www.morning.sfsjh.cn.gov.cn.sfsjh.cn http://www.morning.mdfxn.cn.gov.cn.mdfxn.cn http://www.morning.wfdlz.cn.gov.cn.wfdlz.cn http://www.morning.nnpwg.cn.gov.cn.nnpwg.cn http://www.morning.jggr.cn.gov.cn.jggr.cn