C++的异常处理

这提到抛出引用类异常,可是那个指针在try中就被释放了,后面就出现访问错误了,见下面代码:

#include <cstdio>
using namespace std;

class Ex {
private:
    int *id;
public:
    Ex() {
        id = new int;
    }
    ~Ex() {
        delete id;
        id = NULL;
    }
    int errorId() { return *id; }
};

void fun() throw (Ex, Ex*) {
    Ex *ee = NULL;
    try {
        Ex ex;
        throw &ex;
    } catch (Ex e) {
        printf("catched Ex %d
", e.errorId()); 
    } catch (Ex *e) {
        ee = e;
        printf("catched Ex* %d
", e->errorId());
    } catch (...) {
        printf("catched other.
"); 
    }
    if (ee != NULL) {
        ee->errorId();
        printf("OK
");
    }
}

int main () {
    fun();
}
原文地址:https://www.cnblogs.com/litstrong/p/3323866.html