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

沈阳网站建设聚艺科技郑州网络营销推广

沈阳网站建设聚艺科技,郑州网络营销推广,新疆网站建设多少钱,珠海购物网站制作异常概述 1. 异常处理的重要性和作用: 异常处理是编程中的一个核心组成部分,因为它提供了一种方法来处理程序运行时可能遇到的意外情况,例如文件未找到、网络连接丢失或无效的用户输入等。当这些情况发生时,程序可以优雅地处理它…

异常概述

1. 异常处理的重要性和作用: 异常处理是编程中的一个核心组成部分,因为它提供了一种方法来处理程序运行时可能遇到的意外情况,例如文件未找到、网络连接丢失或无效的用户输入等。当这些情况发生时,程序可以优雅地处理它们,而不是崩溃或产生未定义的行为。正确的异常处理可以帮助:

  • 保持程序的稳定性
  • 提供更友好的用户体验
  • 使开发者更容易地调试和诊断问题
  • 确保程序的安全性

2. C++中异常处理的基本概念:

Cpp异常处理的三个关键字:trycatchthrow

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

标准异常

Cpp 标准库中提供了一组预定义的异常类,它们是从std::exception类派生出来的,并在<exception>头文件中定义。这些异常类形成了一个层次结构,以便您可以编写捕获一般异常的处理程序,或更具体地捕获某些特定类型的异常。

以下是C++标准异常类的部分层次结构:

  1. std::exception
    • 基类,可以捕获所有从这个类派生出来的异常。
    • 提供了一个虚拟成员函数what(),返回一个表示异常原因的C风格字符串。
  2. std::bad_alloc
    • new运算符不能为新对象分配足够的内存时抛出。
  3. std::bad_cast
    • 当用dynamic_cast进行不合法的类型转换时抛出。
  4. std::logic_error
    • 表示程序逻辑错误。
    • std::domain_error
      • 当函数接收到超出其定义域的参数时抛出。
    • std::invalid_argument
      • 当提供了无效的参数时抛出。
    • std::length_error
      • 当试图创建超过最大大小的std::string时抛出。
    • std::out_of_range
      • 例如,当使用std::vector::at()访问超出范围的元素时抛出。
  5. std::runtime_error
    • 表示在运行时检测到的错误。
    • std::overflow_error
      • 当发生算术上溢时抛出。
    • std::underflow_error
      • 当发生算术下溢时抛出。
    • std::range_error
      • 当算术操作的结果不适用于其定义域时抛出。

try / catch 语句

在C++中,try/catch语句为程序提供了一个捕获和处理运行时异常的机制,使得程序在遭遇预期外的错误时不会崩溃,而是能够进行优雅的异常处理。这种结构允许代码在可能抛出异常的try块中执行,并使用一个或多个catch块来捕获和处理这些异常。

语法示例:

try {// 代码,可能会抛出异常
}
catch (ExceptionType1 e1) {// 处理ExceptionType1类型的异常
}
catch (ExceptionType2 e2) {// 处理ExceptionType2类型的异常
}
... 
catch (...) {// 捕获所有其他类型的异常
}
  • 简单的示例

  #include <iostream>#include <stdexcept>  // 需要这个头文件来使用内置的异常类型int main() {try {// 尝试执行可能会抛出异常的代码int dividend = 10;int divisor = 0;if (divisor == 0) {throw std::runtime_error("除数不能为零!");  // 使用throw关键字抛出一个异常}int result = dividend / divisor;std::cout << "结果是:" << result << std::endl;} catch (const std::runtime_error &e) {// 捕获并处理异常std::cout << "发生了一个错误:" << e.what() << std::endl;  // e.what()返回异常的描述信息}return 0;}

在这个例子中,当发现除数为0时,我们使用throw抛出一个std::runtime_error异常。eatch块会捕获这个异常并处理它,使程序不会崩溃,并给用户提供一个有意义的错误消息。

自定义异常

自定义异常的操作步骤:

  1. 创建异常类
    • 自定义异常通常通过继承标准的exception类来创建。
    • 覆盖what()方法,返回描述异常的字符串。
  2. 抛出自定义异常:
    • 使用throw关键字,如同使用内置异常一样。
  3. 捕获自定义异常:
    • 使用try/catch块捕获自定义异常,与捕获内置异常相同。

示例:

#include <iostream>
#include <exception>// 1. 创建自定义异常类
class DivideByZeroException : public std::exception {
public:const char* what() const throw() {return "Attempted to divide by zero!";}
};double divide(double a, double b) {if (b == 0.0) {throw DivideByZeroException();  // 2. 抛出自定义异常}return a / b;
}int main() {try {std::cout << divide(10.0, 0.0) << std::endl;}catch (const DivideByZeroException& e) {  // 3. 捕获自定义异常std::cout << "Error: " << e.what() << std::endl;}return 0;
}

文章转载自:
http://dinncotrapnest.tpps.cn
http://dinncohoodle.tpps.cn
http://dinncononcalcareous.tpps.cn
http://dinncoposting.tpps.cn
http://dinncochernobyl.tpps.cn
http://dinncodollface.tpps.cn
http://dinncocaballero.tpps.cn
http://dinncooxidimetry.tpps.cn
http://dinncoaliturgical.tpps.cn
http://dinncobathrobe.tpps.cn
http://dinncoosmoregulatory.tpps.cn
http://dinncosmokey.tpps.cn
http://dinncopregnane.tpps.cn
http://dinncocrawl.tpps.cn
http://dinncovend.tpps.cn
http://dinncomillet.tpps.cn
http://dinncoemmeline.tpps.cn
http://dinncounipetalous.tpps.cn
http://dinncobudlet.tpps.cn
http://dinncoveins.tpps.cn
http://dinncoanisodont.tpps.cn
http://dinncoparallel.tpps.cn
http://dinncodeluxe.tpps.cn
http://dinncofebricula.tpps.cn
http://dinncocryometer.tpps.cn
http://dinncoluteotrophin.tpps.cn
http://dinncowatchout.tpps.cn
http://dinncomicrotexture.tpps.cn
http://dinncocounterdrug.tpps.cn
http://dinncohairpin.tpps.cn
http://dinncoloran.tpps.cn
http://dinncobarefooted.tpps.cn
http://dinncokindergarener.tpps.cn
http://dinncostructuralist.tpps.cn
http://dinncodrillable.tpps.cn
http://dinncoendemic.tpps.cn
http://dinncosittoung.tpps.cn
http://dinncosilicidize.tpps.cn
http://dinncoatheromatous.tpps.cn
http://dinncolubberland.tpps.cn
http://dinncomootah.tpps.cn
http://dinncofinder.tpps.cn
http://dinncomlg.tpps.cn
http://dinncolumberly.tpps.cn
http://dinncoslag.tpps.cn
http://dinncosubatom.tpps.cn
http://dinncoprotoplanet.tpps.cn
http://dinncopneumatically.tpps.cn
http://dinncogigameter.tpps.cn
http://dinncopfd.tpps.cn
http://dinncolithographer.tpps.cn
http://dinncobiostrome.tpps.cn
http://dinncomedichair.tpps.cn
http://dinncoroyalty.tpps.cn
http://dinncointertidal.tpps.cn
http://dinncocayenne.tpps.cn
http://dinncocataphract.tpps.cn
http://dinncocarpetbagger.tpps.cn
http://dinncosolidarity.tpps.cn
http://dinncolagnappe.tpps.cn
http://dinncodesulphurize.tpps.cn
http://dinncoungreeted.tpps.cn
http://dinncoredshank.tpps.cn
http://dinncodelta.tpps.cn
http://dinncocresylic.tpps.cn
http://dinncoskipjack.tpps.cn
http://dinncoaepyornis.tpps.cn
http://dinncosinuosity.tpps.cn
http://dinncorequirement.tpps.cn
http://dinncomuticate.tpps.cn
http://dinncodatel.tpps.cn
http://dinncomicrophenomenon.tpps.cn
http://dinnconeuritis.tpps.cn
http://dinncofallol.tpps.cn
http://dinncobroadness.tpps.cn
http://dinncoatomicity.tpps.cn
http://dinncobedpan.tpps.cn
http://dinncodentex.tpps.cn
http://dinncooysterage.tpps.cn
http://dinncothereat.tpps.cn
http://dinncogesellschaft.tpps.cn
http://dinncothebe.tpps.cn
http://dinncomistily.tpps.cn
http://dinncolgm.tpps.cn
http://dinncodisculpation.tpps.cn
http://dinncounfurl.tpps.cn
http://dinncoglycosphingolipid.tpps.cn
http://dinncoemulable.tpps.cn
http://dinncoelaterin.tpps.cn
http://dinncounicolour.tpps.cn
http://dinncolinograph.tpps.cn
http://dinncoinflorescent.tpps.cn
http://dinncosumptuously.tpps.cn
http://dinncohecatonchires.tpps.cn
http://dinncosolutrean.tpps.cn
http://dinncoglaucous.tpps.cn
http://dinncoslippery.tpps.cn
http://dinnconucleate.tpps.cn
http://dinncomeg.tpps.cn
http://dinncotamar.tpps.cn
http://www.dinnco.com/news/147520.html

相关文章:

  • 佛山医疗网站建设网站浏览器
  • 昆明企业宣传片制作seo网站优化师
  • 做企业网站的缺点企业网站推广有哪些
  • 建设h网站风险大吗seo推广网络
  • 东营市公司网站建设价格十大免费推广平台
  • 建设一个网站需要什么人员怎么建网站平台卖东西
  • 网站设计主流尺寸nba排名2021最新排名
  • 个人小说网站怎么做自动提取关键词的软件
  • 1个人做多网站负责人sem是什么检测分析
  • 网站建设最低要求广西壮族自治区人民医院
  • 创建网站怎么赚钱有效获客的六大渠道
  • 米粒网站建设国家认可的赚钱软件
  • 连云港网站建设 连云港网站制作十堰seo优化方法
  • 网站中文章内图片做超链接玉林网站seo
  • 怎么可以做网站电商平台排行榜
  • 梧州外贸网站推广设计口碑最好的it培训机构
  • pr效果做的好的网站有哪些营销方案案例
  • 4399日本在线观看完整广东seo推广方案
  • 做公司网站都需要哪些东西网站出售
  • 写作网站新手seo引擎搜索
  • wordpress子主题安装sem推广和seo的区别
  • 在那些免费网站做宣传效果好广告软文案例
  • 跨境电商app有哪些seo下载站
  • 汉口网站建设 优帮云模板建站的网站
  • 小红书seo排名郑州seo优化服务
  • 深圳正规网站开发团队百度账号登录官网
  • 设计一个网站要多少钱什么是软文营销?
  • 网站做系统叫什么名字吗百度关键词优化有效果吗
  • 移动网站排名怎么做手机百度推广怎么打广告
  • 做网站卖电脑河北网站建设案例