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

校园网站建设的意义wordpress文件夹里图片无法获取

校园网站建设的意义,wordpress文件夹里图片无法获取,wordpress虚拟资源下载源码,做简历的什么客网站一、异常处理手段 抛出异常:throw 异常 作用:让调用者看见这个异常,如果调用者不理睬,就让调用者的调用者看见 接住异常: try {可能异常的code} catch(异常类型) {处理方式} 异常类型:一般为const &,防…

一、异常处理手段

  • 抛出异常:throw 异常

    • 作用:让调用者看见这个异常,如果调用者不理睬,就让调用者的调用者看见

  • 接住异常: try {可能异常的code} catch(异常类型) {处理方式}

    • 异常类型:一般为const &,防止copy;

    • 如果 不确定异常类型,也可以用catch(...){处理方式},并且在catch中仍可用throw;继续抛

二、标准异常类

  • 都继承于 exception 这个基类
  • 异常的类型:
    • bad_alloc
    • logic_error 
      • length_error
      • out_of_range (stl容器调用at)
      • domain_error
      • invalid_argument
    • runtime_error
      • range_error
      • overflow_error
      • underflow_error
    • bad_cast
  • catch(...)一般写在异常类型的最后

例子:

#include <iostream>
#include <vector>void 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;
};

五、异常安全保证

  1. 不抛异常(Nothrow guarantee):保证不抛出异常
  2. 强异常安全保证(Strong guarantee):抛出异常程序状态不变
  3. 弱异常安全保证(Weak guarantee):状态改变,但在有效状态

六、不抛出异常 noexcept 关键字可以让编译器进行更好的优化

  • 如果函数声明了noexcept, 但还是抛出了一场,调用栈可能开解(直接崩溃)
    • note:移动构造 和 移动赋值 不声明为noexcept,比那一期是不敢用的
  • noexcept的两种特殊用法:
    • noexcept(bool) : 相当于开关noexcept的作用,noexcept(true) = noexcept(bool);
    • noexcept(noexcept(std::swap(thing,other.thing))) : 这个noexcept是一个表达式。根据内部函数是够抛异常决定返回true/false

七、copy&swap

#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.yayakq.cn/news/43704/

相关文章:

  • 优化网站入口页面的四个维度泰安市人才交流服务中心
  • 网站建设公司公司介绍除了速卖通还有什么网站做外贸
  • 那个公司可以做网站金光华网站建设
  • 网站建设常用的方法孟村住房建设局网站
  • 门户网站开发投标文件湖南企业建网站公司
  • 漳州微网站建设哪家好网站最好服务器
  • asp网站安装到空间如何上传文件到自己的网站
  • 城乡建设部网官方网站工程建设是干什么的
  • 本地数据库搭建网站网站建设从零开始教程
  • 建手机网站要多少钱带商城的wordpress
  • 响应式网站的好处关键词优化诊断
  • 网站建设时间及简介东莞城建局官网
  • 开设赌场罪建设网站东营小程序开发制作
  • 网站主题服务网站制作的步骤
  • 网站建设制作公司都选万维科技企业网站 php 免费
  • 手机网站源码怎么打开网站产品详情页怎么做的
  • 最超值的手机网站建设网站做很久了百度没收录
  • 做擦边球网站网络推广网站 优帮云
  • 美好乡村建设网站做公司网站的费用计入什么科目
  • 网站建站定做页面设计教案
  • 凡客衬衫官方网站织梦手机网站源码下载
  • 网站开发项目可行性分析苏州网站建设系统价格合理
  • 网站开发需要用什么小米发布会时间2022
  • 余姚做网站的公司陈铭生怎么死的
  • 网站开发主要参考文献网站运营之怎样做好seo优化
  • 如何建设个人的网站贵金属交易网站源码
  • 常州知名网站公司旅游网站的建设论文
  • 网站开发培训课程国外html5游戏网站
  • 网站开发云南WordPress主题开发者
  • 网站开发是做什么百度云搜索引擎网站