From 2638de12eefe860bab07e0822bada72e563be1d4 Mon Sep 17 00:00:00 2001 From: p68710245 Date: Sat, 11 May 2024 11:39:16 +0800 Subject: [PATCH] Add exception_without_delete.cpp --- exception_without_delete.cpp | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 exception_without_delete.cpp diff --git a/exception_without_delete.cpp b/exception_without_delete.cpp new file mode 100644 index 0000000..d4f9b81 --- /dev/null +++ b/exception_without_delete.cpp @@ -0,0 +1,38 @@ +#include +#include +using std::cin, std::cout, std::string; + +class A +{ +public: + A() { cout << "A构造\n"; } + ~A() { cout << "A析构\n"; } +}; + +class Test +{ + A *p; + +public: + Test() + { + p = new A[3]; + throw -1; + } + ~Test() + { + delete[] p; + } +}; + +int main() +{ + try + { + Test t{}; + } + catch (int) + { + cout << "捕获了 int异常\n"; + } +} \ No newline at end of file