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\n", e.errorId()); } catch (Ex *e) { ee = e; printf("catched Ex* %d\n", e->errorId()); } catch (...) { printf("catched other.\n"); } if (ee != NULL) { ee->errorId(); printf("OK\n"); } } int main () { fun(); }