#include <iostream> using namespace std; template< typename T> T Div(T a, T b) { if(b==0) throw b; cout<<"End of Div"<<endl; return a/b; } int main() { try { cout<<"1/2="<< Div(1,2)<<endl; cout<<"6.0/0.0="<< Div(6.0,0.0)<<endl; cout<<"End of try"<<endl; } catch(int) { cout<<"Error2: /0 "<<endl; } catch( double ) { cout<<"Error1: /0.0 "<<endl; } cout<<"End of main"<<endl; return 0; }输出什么
时间: 2023-08-20 16:28:31 浏览: 23
输出如下:
```
1/2=0.5
Error1: /0.0
End of main
```
在第一次调用`Div(1,2)`时,会正常执行,并返回0.5。在第二次调用`Div(6.0,0.0)`时,由于除数为0,会抛出一个double类型异常。因此,第二个catch语句会捕获这个异常,并输出"Error1: /0.0"。由于没有其他异常抛出,程序会继续执行,输出"End of main"。注意,catch语句中的参数类型应该与抛出的异常类型一致,否则无法捕获。因此,第一个catch语句无法捕获抛出的异常,不会执行。
相关问题
#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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)