exceptions.cpp

来自「C++高级编程这本书所附的源代码」· C++ 代码 · 共 20 行

CPP
20
字号
#include <stdexcept>
#include <iostream>

void throwIf(bool inShouldThrow) throw (std::runtime_error) 
{
  if (inShouldThrow) {
    throw std::runtime_error("Here's my exception");
  }
}

int main(int argc, char** argv)
{
  try {
    throwIf(false); // doesn't throw
    throwIf(true);  // throws!
  } catch (const std::runtime_error& exception) {
    std::cerr << "Caught exception: " << exception.what() << std::endl;
  }
}

⌨️ 快捷键说明

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