source:
doc/theses/andrew_beach_MMath/code/try-catch.cpp
@
dd78dbc
Last change on this file since dd78dbc was f79ee0d, checked in by , 3 years ago | |
---|---|
|
|
File size: 946 bytes |
Rev | Line | |
---|---|---|
[ea593a3] | 1 | // Cross a Try Statement with a Termination Handler |
[ee23a8d] | 2 | #include <chrono> |
[ea593a3] | 3 | #include <cstdlib> |
[ee23a8d] | 4 | #include <exception> |
5 | #include <iostream> | |
[f79ee0d] | 6 | #include <iomanip> |
[ee23a8d] | 7 | |
[f79ee0d] | 8 | using namespace std; |
[ee23a8d] | 9 | using namespace std::chrono; |
[ea593a3] | 10 | |
11 | struct NotRaisedException : public std::exception {}; | |
12 | ||
13 | int main(int argc, char * argv[]) { | |
14 | unsigned int times = 1; | |
[866cad3] | 15 | volatile bool should_throw = false; |
[ee23a8d] | 16 | if (1 < argc) { |
[ea593a3] | 17 | times = strtol(argv[1], nullptr, 10); |
18 | } | |
19 | ||
[ee23a8d] | 20 | time_point<steady_clock> start_time = steady_clock::now(); |
[ea593a3] | 21 | for (unsigned int count = 0 ; count < times ; ++count) { |
22 | try { | |
[866cad3] | 23 | asm volatile ("# try block"); |
[54651005] | 24 | if (should_throw) { |
25 | throw NotRaisedException(); | |
26 | } | |
[ea593a3] | 27 | } catch (NotRaisedException &) { |
[54651005] | 28 | asm volatile ("# catch block"); |
[ea593a3] | 29 | } |
30 | } | |
[ee23a8d] | 31 | time_point<steady_clock> end_time = steady_clock::now(); |
32 | nanoseconds duration = duration_cast<nanoseconds>(end_time - start_time); | |
[f79ee0d] | 33 | cout << "Run-Time (s): " << fixed << setprecision(1) << duration.count() / 1'000'000'000. << endl; |
[ea593a3] | 34 | } |
Note: See TracBrowser
for help on using the repository browser.