zoukankan      html  css  js  c++  java
  • c++ 异常处理

    #include "stdafx.h"
    #include <iostream>
    using namespace std;

    enum EHstate{ noErr, zeroOP,nega, severeError};
    enum EHstate state = noErr;
    int mathFunc(int i)
    {
    if(i == 0)
    {
    throw state;
    }
    }
    void calculate(int op)
    {
    try
    {
    mathFunc(op);
    }
    catch(EHstate &eobj)//声明为引用
    {
    eobj = severeError;//此处未修改全局变量的值
    }
    }
    int _tmain(int argc, _TCHAR* argv[])
    {
    cout<<"全局变量:"<<state<<endl;
    calculate(0);
    cout<<"全局变量:"<<state<<endl;
    return 0;
    }


    #include "stdafx.h"
    #include <iostream>
    using namespace std;

    enum EHstate{ noErr, zeroOP,nega, severeError};
    enum EHstate state = noErr;
    int mathFunc(int i)
    {
    if(i == 0)
    {
    throw state;
    }
    }
    void calculate(int op)
    {
    try
    {
    mathFunc(op);
    }
    catch(EHstate eobj)//此处为对象,不是引用
    {
    eobj = severeError;//修改此局部对象
    throw;//抛出的仍然是原始的异常对象,而非修改的局部对象
    }
    }
    int _tmain(int argc, _TCHAR* argv[])
    {
    cout<<"全局变量:"<<state<<endl;
    try
    {
    calculate(0);
    }
    catch(EHstate eobj)
    {
    cout<<"捕捉的异常对象值:"<<eobj<<endl;
    }
    cout<<"全局变量:"<<state<<endl;
    return 0;
    }
  • 相关阅读:
    [Linux]yum开启rpm包缓存
    [Linux]centOS7-1-1503-x86_64下安装VM-TOOLS
    [Linux]centOS7下RPM安装Perl
    vue 之 pageage.json 和 webpack.config.js
    node 之 apache
    node 之 express
    node 之 基础知识
    npm nvm nrm的关系
    echarts 学习笔记
    git 操作学习
  • 原文地址:https://www.cnblogs.com/handongdong/p/2223731.html
Copyright © 2011-2022 走看看