#include<iostream> using namespace std; class Exception { public: Exception(){} virtual ~Exception(){} virtual void PrintError() = 0; }; class OutOfMemory :public Exception { public: OutOfMemory(){} ~OutOfMemory(){} virtual void PrintError(); }; void OutOfMemory::PrintError() { cout << "Out of Memory!!\n"; } class RangeError :public Exception { public: RangeError(unsigned long number) { BadNum = number; } ~RangeError(){} virtual void PrintError(); virtual unsigned long GetNumber() { return BadNum; } virtual void SetNumber(unsigned long number) { BadNum = number; } private: unsigned long BadNum; }; void RangeError::PrintError() { cout << "Number out of range.You used " << GetNumber() << " !\n"; } void fn1(); unsigned int* fn2(); void fn3(unsigned int*); int main() { try { fn1(); } catch (Exception& theException) { theException.PrintError(); } return 0; } unsigned int* fn2() { unsigned int* n = new unsigned int; if (n == 0) throw OutOfMemory(); return n; } void fn1() { unsigned int* p = fn2(); fn3(p); cout << "The number is : " << *p << endl; delete p; } void fn3(unsigned int* p) { long Number; cout << "Enter an integer(0~1000):"; cin >> Number; if (Number > 1000 || Number < 0) throw R!!angeError(Number); *p = Number; }分析其中的异常处理
时间: 2024-01-28 16:03:03 浏览: 33
这段代码实现了基于多态的异常处理。它定义了一个 Exception 类作为基类,派生出 OutOfMemory 和 RangeError 两个子类,用于处理内存不足和数字超出范围的异常。
在主函数中,使用 try-catch 语句捕获可能抛出的异常,并将异常对象传递给 catch 块中的 Exception 引用。这里的 Exception 引用是基类的引用,可以指向任何派生类的对象。通过这种方式,程序可以根据抛出的具体异常类型来执行不同的处理逻辑,而不需要在每个函数中都编写相同的异常处理代码。
在 fn1 函数中,它调用了 fn2 函数分配内存,并将返回的指针传递给 fn3 函数。如果 fn2 函数无法分配内存,将抛出 OutOfMemory 异常。如果 fn3 函数接收到的数字超出了范围,将抛出 RangeError 异常。
在每个异常类中,都定义了一个虚函数 PrintError,用于输出异常信息。在捕获到异常后,调用该函数即可输出异常信息,实现了异常的处理和提示。
相关问题
#include<iostream> #include<cstdio> using namespace std;是什么意思
#include<iostream> #include<cstdio> using namespace std; 是C++中的预处理指令,用于引入头文件和命名空间。
1. #include<iostream> 是引入iostream头文件,其中包含了输入输出流的定义,例如cout和cin等。
2. #include<cstdio> 是引入cstdio头文件,其中包含了C语言标准输入输出函数的定义,例如printf和scanf等。
3. using namespace std; 是使用std命名空间,std是C++标准库的命名空间,其中包含了很多常用的函数和类。
这些预处理指令的作用是为了在程序中能够使用输入输出流和标准库函数,使得程序更加方便和简洁。
#include<iostream>using namespace std;
这是一个C++的代码片段,它包含了iostream头文件和使用了命名空间std。iostream头文件提供了输入输出流的功能,而using namespace std;语句则是为了方便使用std命名空间中的函数和对象,避免每次都需要写std::。
以下是一个简单的示例代码,展示了如何使用iostream头文件和命名空间std来输出一句话:
```cpp
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
```
这段代码会输出"Hello, World!",endl是一个特殊的控制符,用于在输出流中插入一个换行符。
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)