update exceptions example
This commit is contained in:
@@ -44,7 +44,8 @@
|
|||||||
"stdexcept": "cpp",
|
"stdexcept": "cpp",
|
||||||
"streambuf": "cpp",
|
"streambuf": "cpp",
|
||||||
"cinttypes": "cpp",
|
"cinttypes": "cpp",
|
||||||
"typeinfo": "cpp"
|
"typeinfo": "cpp",
|
||||||
|
"csignal": "cpp"
|
||||||
},
|
},
|
||||||
"C_Cpp.errorSquiggles": "disabled"
|
"C_Cpp.errorSquiggles": "disabled"
|
||||||
}
|
}
|
||||||
@@ -13,9 +13,8 @@
|
|||||||
#include <retarget.hpp>
|
#include <retarget.hpp>
|
||||||
|
|
||||||
template <class T, std::size_t N> struct ring_buffer {
|
template <class T, std::size_t N> struct ring_buffer {
|
||||||
|
|
||||||
std::array<T, N> arr;
|
std::array<T, N> 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) {
|
void push(T t) {
|
||||||
arr.at(write_idx++) = t;
|
arr.at(write_idx++) = t;
|
||||||
@@ -35,30 +34,34 @@ int main()
|
|||||||
printf("Exceptions example\r\n");
|
printf("Exceptions example\r\n");
|
||||||
|
|
||||||
std::set_terminate([]() {
|
std::set_terminate([]() {
|
||||||
printf("Unhandled exception!\r\n");
|
printf("My terminate handler!\r\n");
|
||||||
while(true){}
|
while(true){}
|
||||||
});
|
});
|
||||||
|
|
||||||
std::array<int, 4> arr;
|
std::array<int, 4> arr;
|
||||||
|
|
||||||
|
// uncomment the following line to trigger terminate handler
|
||||||
// arr.at(5) = 6;
|
// arr.at(5) = 6;
|
||||||
|
|
||||||
//throw 1;
|
|
||||||
try {
|
try {
|
||||||
//throw 1;
|
|
||||||
arr.at(5) = 6;
|
arr.at(5) = 6;
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (int i) {
|
catch(std::out_of_range &e) {
|
||||||
printf("i = %d\r\n", i);
|
printf("Array out of range!\r\n");
|
||||||
}
|
|
||||||
catch(std::out_of_range) {
|
|
||||||
printf("out of range\r\n");
|
|
||||||
}
|
}
|
||||||
catch (...) {
|
catch (...) {
|
||||||
printf("Catch\r\n");
|
printf("Unexpected expection...\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ring_buffer<int, 4> 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)
|
while(true)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user