diff --git a/Chapter07/error_handling/.vscode/settings.json b/Chapter07/error_handling/.vscode/settings.json index b453fcd..ce9eb18 100644 --- a/Chapter07/error_handling/.vscode/settings.json +++ b/Chapter07/error_handling/.vscode/settings.json @@ -44,7 +44,8 @@ "stdexcept": "cpp", "streambuf": "cpp", "cinttypes": "cpp", - "typeinfo": "cpp" + "typeinfo": "cpp", + "csignal": "cpp" }, "C_Cpp.errorSquiggles": "disabled" } \ No newline at end of file diff --git a/Chapter07/error_handling/app/src/main_exceptions.cpp b/Chapter07/error_handling/app/src/main_exceptions.cpp index 4bd5cd1..7605d28 100644 --- a/Chapter07/error_handling/app/src/main_exceptions.cpp +++ b/Chapter07/error_handling/app/src/main_exceptions.cpp @@ -13,9 +13,8 @@ #include template struct ring_buffer { - std::array arr; - std::size_t write_idx = 0; // Index of the next element to write (push) + std::size_t write_idx = 0; void push(T t) { arr.at(write_idx++) = t; @@ -35,30 +34,34 @@ int main() printf("Exceptions example\r\n"); std::set_terminate([]() { - printf("Unhandled exception!\r\n"); + printf("My terminate handler!\r\n"); while(true){} }); std::array arr; - //arr.at(5) = 6; + // uncomment the following line to trigger terminate handler + // arr.at(5) = 6; - //throw 1; try { - //throw 1; arr.at(5) = 6; - } - catch (int i) { - printf("i = %d\r\n", i); - } - catch(std::out_of_range) { - printf("out of range\r\n"); + catch(std::out_of_range &e) { + printf("Array out of range!\r\n"); } catch (...) { - printf("Catch\r\n"); + printf("Unexpected expection...\r\n"); } - + + ring_buffer rb; + try { + for(int i = 0; i < 6; i++) { + rb.push(i); + } + } + catch(std::out_of_range &e) { + printf("Ring buffer out of range!\r\n"); + } while(true) {