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 使用企业库异常处理应用程序模块的 ...
随机推荐
- OO ALV 学习参考
http://blog.csdn.net/sapliumeng/article/details/18653491 一.ALV介绍 The ALV Grid Control (ALV = SAPLi ...
- JNI-使用RegisterNatives注册本地方法
转自: http://blog.chinaunix.net/uid-26009923-id-3410141.html 1. 以前在jni中写本地方法时,都会写成 Java_com_example_he ...
- UITableview 中获取非选中的cell
实现效果如图: 在cell中有一个button,选中cell改变button的选择状态 yes,选中另外一个cell,别的cell中的button选择状态变成false. //获取当前可显示的cell ...
- 访谈将源代码的函数 strcpy/memcpy/atoi/kmp/quicksort
一.社论 继上一次发表了一片关于參加秋招的学弟学妹们怎样准备找工作的博客之后,反响非常大.顾在此整理一下,以便大家复习.好多源自july的这篇博客,也有非常多是我自己整理的.希望大家可以一遍一遍的写. ...
- 一天JavaScript示例-在功能上的标量参数和数组参数的差异
<!DOCTYPE> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta h ...
- android134 360 07 归属地查询
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- procps工具集 ----Linux中的可用内存指的是什么?
https://gitlab.com/procps-ng/procps free - Report the amount of free and used memory in the system k ...
- LayoutInflater
Android中LayoutInflater的使用 博客分类: Android Inflater英文意思是膨胀,在Android中应该是扩展的意思吧. LayoutInflater的作用类似于 f ...
- 如何强化 TCP/IP 堆栈
TCP/IP 是一种本质上不安全的协议.但是,Windows 2000 实现可以使您配置其操作以防止网络的拒绝服务攻击.默认情况下,本文中所涉及的一些项和值可能并不存在.在这些情况下,请创建该项.值或 ...
- Java基础知识强化之网络编程笔记10:TCP之客户端读取文本文件服务器控制台输出
1. TCP之客户端读取文本文件服务器控制台输出 (1)客户端:(发送数据到服务端) package cn.itcast_10; import java.io.BufferedReader; impo ...