From db2f1c3d9121cdda51b24eea1cb3130515ce6041 Mon Sep 17 00:00:00 2001 From: kang sheng Date: Wed, 9 Sep 2020 20:52:33 +0800 Subject: [PATCH] combine error handler with virtual function. --- .../10_day_practice/day9/异常例子/9-2.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 practical_exercises/10_day_practice/day9/异常例子/9-2.cpp diff --git a/practical_exercises/10_day_practice/day9/异常例子/9-2.cpp b/practical_exercises/10_day_practice/day9/异常例子/9-2.cpp new file mode 100644 index 0000000..6b9af7c --- /dev/null +++ b/practical_exercises/10_day_practice/day9/异常例子/9-2.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; +class BasicException +{ +public: + virtual string Where() { return "BasicException..."; } +}; +class FileSysException : public BasicException +{ +public: + virtual string Where() { return "FileSysException..."; } +}; +class FileNotFound : public FileSysException +{ +public: + virtual string Where() { return "FileNotFound..."; } +}; +class DiskNotFound : public FileSysException +{ +public: + virtual string Where() { return "DiskNotFound..."; } +}; +int main() +{ + try + { + // ..... //程序代码 + DiskNotFound err; + throw &err; + } + catch (BasicException *p) + { + cout << p->Where() << endl; + } +}