在C++中引入了三种操作符来处理程序的出错情况,分别是:try  , throw  ,  catch

1.基本的用法如下:

try{
//code to be tried
throw exception;
}
catch(type exception)
{
//code to be executed in case of exception
}

操作过程为:

  (1)try语句块中的代码正常执行,当有异常发生时,代码使用关键字 throw 和一个参数来抛出一个异常,这个参数可以是任何有效的数据类型,它反映了异常的特征;

  (2)当异常发生时,即try语句块中有一条throw被执行时,catch语句块亦即被执行,接受来自throw抛出的参数

Demo:

#include<iostream>
using namespace std;
int main()
{
try
{
char * mystring;
mystring = new char[];
if (mystring == NULL)
throw "Allocate Failure";
for (int i = ;i <= ;i++)
{
if (i > ) throw i;
mystring[i] = 'a';
}
}
catch (int i) //当throw 抛出的参数为int类型时执行
{
cout << "Exception: Index " << i << " is out of range." << endl;
}
catch (char* str) //当throw抛出的参数为char*(string)类型时执行
{
cout << "Exception: " << str << endl;
}
system("pause");
return ;
}

result:

C++异常处理(Exception Handling)的更多相关文章

  1. Akka(26): Stream:异常处理-Exception handling

    akka-stream是基于Actor模式的,所以也继承了Actor模式的“坚韧性(resilient)”特点,在任何异常情况下都有某种整体统一的异常处理策略和具体实施方式.在akka-stream的 ...

  2. Exception Handling Considered Harmful

    异常处理被认为存在缺陷 Do, or do not. There is no try. - Yoda, The Empire Strikes Back (George Lucas) by Jason ...

  3. 异常处理与MiniDump详解(3) SEH(Structured Exception Handling)

    write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie 讨论新闻组及文件 一.   综述 SEH--Structured Exception Handlin ...

  4. Exception handling 异常处理的本质

    异常处理的本质:状态回滚或者状态维护. https://en.wikipedia.org/wiki/Exception_handling In general, an exception breaks ...

  5. C#编程.异常处理(Exception Handling Statements)

    C#语言包含结构化异常处理(Structured Exception Handling,SEH). throw The throw statement is used to signal the oc ...

  6. Exception Handling in ASP.NET Web API webapi异常处理

    原文:http://www.asp.net/web-api/overview/error-handling/exception-handling This article describes erro ...

  7. C# to IL 10 Exception Handling(异常处理)

    Exception handling in IL is a big let down. We expected a significant amount of complexity,but were ...

  8. Exception Handling引入MVP

    异常处理(Exception Handling)是所有系统的最基本的基础操作之一,其它的比如日志(Logging).审核(Auditing).缓存(Caching).事务处理(Transaction) ...

  9. Unity、Exception Handling引入MVP

    什么是MVP?在“MVP初探”里就有讲过了,就是一种UI的架构模式. 简单的描述一下Unity和Exception Handling Application Block: Unity是一个轻量级的可扩 ...

  10. 黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block 使用企业库异常处理应用程序模块的 ...

随机推荐

  1. 【Stage3D学习笔记续】山寨Starling(三):Starling核心渲染流程

    这篇文章我们剔除Starling的Touch事件体系和动画体系,专门来看看Starling中的渲染流程实现,以及其搭建的显示列表结构. 由于Starling是模仿Flash的原生显示列表,所以我们可以 ...

  2. Myeclipse:No projects are available for deployment to this server!

    这是因为以前的项目不是用myEclipse创建的,所以用myeclipse deploy的时候找不到你的项目. 可以这样做:右击原项目名 - myeclipse - Add myeclipse  We ...

  3. 创建性能监视器(logman)

    在本地计算机上抓取性能信息 Logman.exe create counter Perf-1Second -f bincirc -max 500 -c "\Processor(*)\% Pr ...

  4. 【44】将与参数无关的代码抽离templates

    1.template是产生代码的代码,这就意味着源码看起来很少,生成的目标码大量膨胀. 2.考虑,如果两个方法有重复代码,我们会新建一个方法,把重复的代码放进去,原先两个方法调用第三个方法.如果两个类 ...

  5. Codeforces Round #324 (Div. 2) B. Kolya and Tanya 快速幂

    B. Kolya and Tanya Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/pro ...

  6. firefly的rpc。。

    firefly使用了twisted的pb 来实现rpc: http://twistedmatrix.com/documents/current/core/howto/pb-usage.html 服务端 ...

  7. ajax的一部分知识

    jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(p ...

  8. iOS之用xib给控件设置圆角、边框效果

    xib中为各种控件设置圆角 通过代码的方式设置 @interface ViewController () @property (weak, nonatomic) IBOutlet UIView *my ...

  9. Centos7 安装mongodb3.2.9 过程

    1:wget --no-check-certificate  https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-3.2.9.tg ...

  10. Python基础:1.数据类型(元组)

    提示:python版本为2.7,windows系统 1.元组(Tuple) Tuple,与List类似,但是Tuple一旦初始化之后就不能修改了,没有增加.删除.修改元素. >>> ...