Untitled
unknown
c_cpp
5 years ago
863 B
7
Indexable
#include <iostream>
#include <random>
void doJobWithRandomFail() {
std::random_device rd;
std::default_random_engine gen = std::default_random_engine(rd());
std::uniform_int_distribution<int> dis(1,10);
if (dis(gen) < 8) {
throw std::runtime_error("Fail to do the job");
}
}
void doJobWithRetry(int max_tries) {
bool success = false;
for (int count=0; success == false && count <= max_tries; count++) {
std::cout << "Do job, try count: " << count << std::endl;
try {
doJobWithRandomFail();
} catch (const std::runtime_error &err) {
continue;
}
success=true;
}
}
int main() {
doJobWithRetry(20);
return 0;
}Editor is loading...