qfedu-cpp-level/day8/homework/h2.cpp

35 lines
774 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// throw() 和throw有什么区别请举例说明
// 1. throw()是函数声明,表示该函数不会抛出任何异常
// 2. throw是抛出异常的关键字
// 3. throw()是函数声明throw是语句
#include <bits/stdc++.h>
using namespace std;
void show(int n) throw() // throw() 表示异常抛出限制: 不抛出任何异常
// void show(int n) throw(int ) // throw(int) 表示只抛出 int 类型的异常
{
if (n == 0)
{
throw 0; // throw 0 表示抛出 int 类型的异常
}
cout << "n = " << n << endl;
}
int main()
{
try
{
show(0);
}
catch (const int &e)
{
cout << "捕获异常: " << e << endl;
}
catch (...)
{
cout << "捕获其他异常" << endl;
}
return 0;
}