在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. AS问题解决系列1—Unable to execute DX错误

    http://my.oschina.net/1pei/blog/478968 摘要 在将一个开源Android代码import到Android Studio 1.2.2中时,解决了编译期间出现的“Un ...

  2. VM使用标准交换机

    1.新建模板: 网卡选择“未连接”,此处看不到“标准交换机”选项

  3. setsockopt()使用方法(參数具体说明)

    int setsockopt(SOCKET s,int level,int optname,const char* optval,int optlen); s(套接字): 指向一个打开的套接口描写叙述 ...

  4. Python 对新浪微博的博文元素 (Word, Screen Name)的频率分析

    CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-7-9 @author: guaguastd @name: we ...

  5. SQLSERVER复制表的方法

    1.复制表结构及数据     格式:select * into 新表名 from 要复制的表名     --例如:select * into temp from users 2.只复制表数据   格式 ...

  6. [Webpack] Use the Webpack Dashboard to Monitor Webpack Operations

    Learn how to use the new Webpack Dashboard from Formidable Labs to display a pretty, useful output f ...

  7. Linux MySQL-Workbench安装

    yum install pcre-devel libglade2-devel gtkmm24-devel libgnome-devel lua-devel libzip-devel mysql-dev ...

  8. debian配置简单的vsftp服务器

    Ubuntu Linux与Windows系统不同,Ubuntu Linux不会产生无用垃圾文件,但是在升级缓存中,Ubuntu Linux不会自动删除这些文件,今天就来说说这些垃圾文件清理方法.  1 ...

  9. java_不知道数据类型情况下,数组遍历-反射

    if(items.getClass().isArray){ this.collection = new ArrayList(); int length = Array.getLength(items) ...

  10. 你不知道的 Javascript

    作用域 词法作用域:编译阶段确定(欺骗词法作用域 eval with) function foo(str){ "use strict" eval(str) console.log( ...