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;
    }
  • 相关阅读:
    leetcode Super Ugly Number
    leetcode Find Median from Data Stream
    leetcode Remove Invalid Parentheses
    leetcode Range Sum Query
    leetcode Range Sum Query
    leetcode Minimum Height Trees
    hdu 3836 Equivalent Sets
    hdu 1269 迷宫城堡
    hud 2586 How far away ?
    poj 1330 Nearest Common Ancestors
  • 原文地址:https://www.cnblogs.com/handongdong/p/2223731.html
Copyright © 2011-2022 走看看