source:
doc/theses/andrew_beach_MMath/code/cross-catch.cpp
@
8a1d95af
Last change on this file since 8a1d95af was 866cad3, checked in by , 3 years ago | |
---|---|
|
|
File size: 872 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> | |
6 | ||
7 | using namespace std::chrono; | |
[ea593a3] | 8 | |
9 | struct NotRaisedException : public std::exception {}; | |
10 | ||
11 | int main(int argc, char * argv[]) { | |
12 | unsigned int times = 1; | |
[866cad3] | 13 | volatile bool should_throw = false; |
[ee23a8d] | 14 | if (1 < argc) { |
[ea593a3] | 15 | times = strtol(argv[1], nullptr, 10); |
16 | } | |
17 | ||
[ee23a8d] | 18 | time_point<steady_clock> start_time = steady_clock::now(); |
[ea593a3] | 19 | for (unsigned int count = 0 ; count < times ; ++count) { |
20 | try { | |
[866cad3] | 21 | asm volatile ("# try block"); |
[54651005] | 22 | if (should_throw) { |
23 | throw NotRaisedException(); | |
24 | } | |
[ea593a3] | 25 | } catch (NotRaisedException &) { |
[54651005] | 26 | asm volatile ("# catch block"); |
[ea593a3] | 27 | } |
28 | } | |
[ee23a8d] | 29 | time_point<steady_clock> end_time = steady_clock::now(); |
30 | nanoseconds duration = duration_cast<nanoseconds>(end_time - start_time); | |
31 | std::cout << "Run-Time (ns): " << duration.count() << std::endl; | |
[ea593a3] | 32 | } |
Note: See TracBrowser
for help on using the repository browser.