⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 prog17_02.cpp

📁 c++最经典的入门书籍
💻 CPP
字号:
// Program 17.2 Throwing exceptions in nested try blocks  File: prog17_02.cpp
#include <iostream>
using std::cout;
using std::endl;

void throwIt(int i) {
  throw i;                                // Throws the parameter value
}

int main() {
  for(int i = 0 ; i <= 5 ; i++)   {
    try {
      cout << endl << "outer try: ";
      if(i == 0)
        throw i;                          // Throw int exception

      if(i == 1)
        throwIt(i);                       // Call the function that throws int

      try {                               // Nested try block
        cout << endl << " inner try: ";
        if(i == 2)
          throw static_cast<long>(i);     // Throw long exception

        if(i == 3)
          throwIt(i);                     // Call the function that throws int
      }                                   // End nested try block
      catch(int n) {
        cout << endl << "Catch int for inner try. "
             << "Exception " << n;
      }

      cout << endl << "  outer try: ";

      if(i == 4)
        throw i;                          // Throw int
      throwIt(i);                         // Call the function that throws int
    }
    catch(int n) {
      cout << endl << "Catch int for outer try. "
           << "Exception " << n;
    }
    catch(long n) {
      cout << endl << "Catch long for outer try. "
           << "Exception " << n;
    }
  }

  cout << endl;
  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -