【翁恺】33-异常的抛出和捕捉

这篇博客探讨了在C++中如何处理模板类`Vector`中数组索引越界的问题。提出了四种不同的处理方式,包括返回随机内存对象、返回特殊错误值、程序直接退出以及优雅地抛出异常。作者建议使用异常处理,因为这可以将错误处理的责任转移到调用者,并详细解释了如何定义和抛出`VectorIndexError`异常。同时,通过示例展示了不同情况下调用者如何捕获和处理这些异常。
摘要由CSDN通过智能技术生成

example:Vector

template <class T> class Vector{
private:
    T* m_elements;
    int m_size;
public:
    Vector (int size = 0):m_size(size)...
    ~Void(){delete [] m_elements;}
    void length(int);
    int length(){return m_size;}
    T& operator[](int);
};

problem

template <class T>
T& Vector<T>::operator[](int indx){}
  • what should the [] operator do if the index is not valid?  index 越界怎么办?

    1.)return random memory object   不可以

    return m_elements[indx];

    2.)return a special error value   返回一个特殊错误值  不是好办法

    if (indx < 0 || indx >= m_size){
        T* error_marker = new T("some magic value");
        return *error_marker;
    }
    return m_elements[indx];

    *but this throws the baby out with the bath!

    x = v[2] + v[4];      //not safe code

    3.)just die!    

    if (indx < 0 || indx >= m_size){
        exit(22);
    }
    return m_elements[indx];

    4.)die gracefully (with autopsy!)

    assert(indx >= 0 && indx < m_size);  // 不合适
    retrun m_elements[indx];

when to use exceptions  好的解决办法

  • many times ,you don't know what should be done

  • if you do anything you'll be wrong

  • solution:turf the problem

    make your caller(or its caller...) responsible

how to raise an exception

template<class T>
    T& Vector<T>::operator[](int indx){
        if(indx < 0 || indx >= m_size){
            //throw is a keyword
            //exception is raised at this point
            throw --someting--
        }
        return m_elements[indx];
    }

what do you throw?

//what do you have? Data!

//define a class to represent the error

class VectorIndexError{
public:
    VectorIndexError(int v):m_badValue{}
    ~VectorIndexError(){}
    void diagnostic(){
        cerr << "index" << m_badValue <<"out of range";
    }
private:
    int m_badValue;
};

how to raise an exception

template<class T>
T& Vector<T>::operator[](int indx){
    if(indx < 0 || indx >= m_size){
        //VectorIndexError
        //throw e;
        throw VectorIndeError(indx); // VectorIndeError对象在堆栈里面
    }
    return m_elements[indx];
}

what about your caller?

  • case 1 Doesn't care

    • code never even suspects a problem

      int func(){
          Vector<int> v(12);
          v[3] = 5;
          int i = v[42];//out of range
          //control never gets here!
          return i*5;
      }
  • case2 cares deeply

    void outer(){
        try{
            func(); func2();  // func2 不执行
        }catch(VectorIndexError& e){
            e.diagnostic();
            //this excpetion does not propagete
        }
        cout << "control is here after exception"
    }
  • case3 mildly interested

    void outer2(){
        String err("exception caught");
        try{
            func();
        }catch(VextorIndexError){
            cout << err;
            throw;  //propagate the exception
        }
    }
  • case4 does't care about the particulars

    void outer3(){
        try {
            outer2();
        }catch(...){ // 捕捉所有异常
            //...catches **all** exceptions!
            cout << "the exception stops here"
        }
    }

what happened?

review

  • throw statement raise the exception
    • control propagates back to first handler for that exception
    • propagation follows the call chain
    • objects on stack are properly destroyed
  • "throw exp"
    • throws values for matching
  • "throw"
    • reraises the exception being handled
    • valid only within a handler

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

理心炼丹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值