main.cpp
来自「一本语言类编程书籍」· C++ 代码 · 共 41 行
CPP
41 行
// Exercise 17.1 Throwing and catching CurveBalls
#include "CurveBall.h"
#include <cstdlib>
#include <ctime>
#include <iostream>
using std::cout;
using std::endl;
// Function to generate a random integer 0 to count-1
int random(int count) {
return static_cast<int>((count*static_cast<long>(rand()))/(RAND_MAX+1L));
}
// Throw a CurveBall exception 25% of the time
// Note: Some compilers do not support exception specifications for functions
// If yours does not you may need to remove the exception specifications from
// sometimes() and the what() member of CurveBall.
void sometimes() throw(CurveBall) {
CurveBall e;
if(random(20)<5)
throw e;
}
int main() {
std::srand((unsigned)std::time(0)); // Seed random number generator
int number = 1000; // Number of loop iterations
int exceptionCount = 0; // Count of exceptions thrown
for(int i = 0 ; i<number ; i++)
try {
sometimes();
}
catch(CurveBall& e) {
exceptionCount++;
}
cout << "\nCurveBall exception thrown " << exceptionCount << " times out of "
<< number << ".\n";
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?