#include<iostream>
#include<stdexcept>
//exception/stdexcept/new/type_info头文件里都有定义的标准异常类
using namespace std;
int main()
{
try{
int a,b; char s;
cin>>a>>s>>b;
if(s=='/'){
if(b==0) throw "Divided by 0!";
cout<<a<<"/"<<b<<"="<<a/b<<endl;
}
else
if(s=='%') {
if(b==0) throw a;
cout<<a<<"%"<<b<<"="<<a%b<<endl;
}
else
cout<<"Option must be % or /."<<endl;
}//try
//捕获int类型的异常并处理
catch(int i) { cout<<"Error occur:"<<i<<"%0"<<endl; }
//捕获char* 类型的异常并处理
catch(char *str) {cout<<"Error occur:"<<str<<endl; }
catch(runtime_error err){ cout<<err.what()<<endl;}
//捕获其他不论什么异常并处理
catch(...){cout<<"Unkown Error"<<endl;}
//没错误不执行catch。有错误至多执行一个catch语句块,且不再返回try语句块中。
//无论有无异常都要执行到此处
cout<<"Hello world"<<endl;
return 0;
}

C++——try、throw、catch实例学习程序的更多相关文章

  1. c++ try throw catch

    c++ try throw catch 这三者联合使用 , try { statement list; } catch( typeA arg ) { statement list; } catch( ...

  2. C++中的try throw catch 异常处理

    今天在开发过程中调用一个库函数结果库函数有throw操作,当前代码没有对throw进行捕获操作,导致进程在main 函数中捕获到异常导致进程crash.所以借此记录下c++关于try,throw,ca ...

  3. try throw catch

    #include "stdafx.h" #include <iostream> #include <stdlib.h> using namespace st ...

  4. try throw catch异常处理机制

    /*本程序实现分块查找算法  又称索引顺序查找     需要注意的是分块查找需要2次查找  先对块查找  再对块内查找    2013.12.16    18:44*/ #include <io ...

  5. try throw catch typeid

    QString str = ui.ll->text(); try { if (str == NULL) { throw 1; } else { throw 1.2; } } catch (int ...

  6. 132.try throw catch介绍

    #include <iostream> using namespace std; //try尝试执行,抛出throw,throw之后语句不再执行 //catch处理throw的异常 voi ...

  7. 存储过程,游标,异常捕捉 try catch 实例代码

    1.存储过程代码.  (数据库是 AdventureWorks) 存储过程用来将错误信息插入到指定的表中. 在别的地方可以调用存储过程插入错误信息(下面部分代码 生成一个表,如果表已经存在就会插入错误 ...

  8. C#中try catch中throw ex和throw方式抛出异常有何不同

    我们在C#的try catch代码块中里面经常使用throw语句抛出捕捉到的异常,但是你知道吗使用throw ex和throw抛出捕获到的异常效果是不一样的. 异常捕捉的原理 首先先介绍一下C#异常捕 ...

  9. 【又长见识了】C#异常处理,try、catch、finally、throw

    异常处理:程序在运行过程中,发生错误会导致程序退出,这种错误,就叫做异常.处理这种错误,就叫做异常处理. 1.轻描淡写Try.Catch.Finally.throw用法 在异常处理中,首先需要对可能发 ...

随机推荐

  1. [ES6] Rest Parameter

    Problem with the ES5: function displayTags(){ for (let i in arguments) { let tag = arguments[i]; _ad ...

  2. C++沉思录之一

    一.系统跟踪类Trace的设计过程: step1:简单版 class Trace { public: Trace() { noisy = ; } void print(char* s) { if(no ...

  3. Java基础知识强化35:String类之String的其他功能

    1. String类的其他功能: (1)替换功能: String replace(char old, char new) String replace(String old,String new) ( ...

  4. css圆角

    在CSS3中圆角属性,有四个.三个.两个和一个值. 四个值: 第一个值为左上角,第二个值为右上角,第三个值为右下角,第四个值为左下角.

  5. Function.prototype.bind

    解析Function.prototype.bind 简介 对于一个给定的函数,创造一个绑定对象的新函数,这个函数和之前的函数功能一样,this值是它的第一个参数,其它参数,作为新的函数的给定参数. b ...

  6. C#中的switch case

    在C#中switch(type){case tpye1:break;case tpye2:break;case tpye3:break;case tpye4:break;};其中type可以是数字,也 ...

  7. sql 字段字符串内容替换

    SELECT * FROM dbo.Table WHERE Name LIKE '%NYCL23%'UPDATE Table SET Name=replace(Name,'NYCL23','WYCL1 ...

  8. java下管道流 PipedOutputStream 与PipedInputStream

    package cn.stat.p2.demo; import java.io.IOException; import java.io.PipedInputStream; import java.io ...

  9. angularjs使用ng-messages的注册表单实例

    <!DOCTYPE html> <html lang="zh-CN" ng-app="app"> <head> <me ...

  10. sass基础学习

    2015.6.281.安装ruby2.运行gem安装sass-->gem install sass3.编译命令行sass --watch 文件路径/test.scss:编译后文件路径/test. ...