C++异常处理(Exception Handling)
在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)的更多相关文章
- Akka(26): Stream:异常处理-Exception handling
akka-stream是基于Actor模式的,所以也继承了Actor模式的“坚韧性(resilient)”特点,在任何异常情况下都有某种整体统一的异常处理策略和具体实施方式.在akka-stream的 ...
- Exception Handling Considered Harmful
异常处理被认为存在缺陷 Do, or do not. There is no try. - Yoda, The Empire Strikes Back (George Lucas) by Jason ...
- 异常处理与MiniDump详解(3) SEH(Structured Exception Handling)
write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie 讨论新闻组及文件 一. 综述 SEH--Structured Exception Handlin ...
- Exception handling 异常处理的本质
异常处理的本质:状态回滚或者状态维护. https://en.wikipedia.org/wiki/Exception_handling In general, an exception breaks ...
- C#编程.异常处理(Exception Handling Statements)
C#语言包含结构化异常处理(Structured Exception Handling,SEH). throw The throw statement is used to signal the oc ...
- Exception Handling in ASP.NET Web API webapi异常处理
原文:http://www.asp.net/web-api/overview/error-handling/exception-handling This article describes erro ...
- C# to IL 10 Exception Handling(异常处理)
Exception handling in IL is a big let down. We expected a significant amount of complexity,but were ...
- Exception Handling引入MVP
异常处理(Exception Handling)是所有系统的最基本的基础操作之一,其它的比如日志(Logging).审核(Auditing).缓存(Caching).事务处理(Transaction) ...
- Unity、Exception Handling引入MVP
什么是MVP?在“MVP初探”里就有讲过了,就是一种UI的架构模式. 简单的描述一下Unity和Exception Handling Application Block: Unity是一个轻量级的可扩 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block 使用企业库异常处理应用程序模块的 ...
随机推荐
- Linux安装JDK详细步骤
Linux安装JDK步骤 1.先从网上下载jdk(jdk-7u1-linux-i586.rpm),下载地址:http://www.oracle.com/technetwork/java/javase/ ...
- struts2标签具体解释
要在jsp中使用Struts2的标志,先要指明标志的引入.通过jsp的代码的顶部增加下面的代码: <%@taglib prefix="s" uri="/struts ...
- Upgrading to MySQL 5.7---focusing on temporal types
https://www.percona.com/blog/2016/04/27/upgrading-to-mysql-5-7-focusing-on-temporal-types/ http://ww ...
- 如何安装SQL Server 2008数据库(带完整图解)
在电脑上安装SQL Server 2008 软件时,经常会遇到各种各样的问题,如何成功的安装SQL Server 2008呢?提供完整过程和图片详解. 工具/原料 电脑一台 软件:sql server ...
- Ubuntu10.04下载并编译Android4.3源代码
注:转载或引用请标明出处 http://blog.csdn.net/luzhenrong45/article/details/9719433 去年用Ubuntu10.10成功下载并编译Andro ...
- Apache的RewriteRule规则详细介绍
R[=code](force redirect) 强制外部重定向 (rkyW z强制在替代字符串加上http://thishost[:thisport]/前缀重定向到外部的URL.如果code不指定, ...
- linux 命令学习日记 20160621
1.cat /proc/meminfo 观察Linux系统上虚拟内存的当前状态 2.ipcs -m ipcs命令专门用来查看系统上的当前共享内存页面 3.ls /dev -al sda* ttyS* ...
- VIM 分割窗口
VIM 分割窗口 *08.1* 分割窗口 打开新窗口最简单的命令如下: :split 这个命令把屏幕分解成两个窗口并把光标置于上面的窗口中: +----------------------- ...
- WIndows 7 与 Debian 7 双系统启动引导
以前写有一篇文章是Windows XP下引导Linux的(http://www.cnblogs.com/ibgo/archive/2012/05/20/2510736.html),采用的是grub4d ...
- 开源Web安全测试工具调研
开源Web安全测试工具调研 http://blog.csdn.net/testing_is_believing/article/details/22302087