From b0a3dd24602320baabb45a075c00a4fb50056188 Mon Sep 17 00:00:00 2001 From: Vahagn Khachatryan Date: Wed, 17 Oct 2018 12:56:45 -0400 Subject: [PATCH] exception_context.cpp --- cpp/cpp11/exception_context.cpp | 57 +++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 cpp/cpp11/exception_context.cpp diff --git a/cpp/cpp11/exception_context.cpp b/cpp/cpp11/exception_context.cpp new file mode 100644 index 0000000..31e3a2e --- /dev/null +++ b/cpp/cpp11/exception_context.cpp @@ -0,0 +1,57 @@ +/* +VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I." +VIM: let b:wcppflags="/O2 /EHsc /DWIN32" +VIM-: let b:cppflags=g:Iboost.g:Itbb +VIM-: let b:ldflags=g:Lboost.g:Ltbb +VIM-: let b:ldlibpath=g:Bboost.g:Btbb +VIM-: let b:argv="" +*/ +#include +#include +#include +#include + +void print_exception() +{ + try { + std::rethrow_exception(std::current_exception()); + } catch ( const std::exception& e ) { + std::cout << e.what() << std::endl; + } +} + +int main ( void ) +{try{ + auto begin = std::chrono::high_resolution_clock::now(); + + try { + throw std::runtime_error("first exception"); + } catch(...) { + try { + throw std::runtime_error("second exception"); + } catch (...) { + print_exception(); + } + print_exception(); + } + + //...... + + auto end = std::chrono::high_resolution_clock::now(); + std::chrono::duration seconds = end - begin; + std::cout << "Time: " << seconds.count() << std::endl; + return 0; +} +catch ( const std::exception& e ) +{ + std::cerr << std::endl + << "std::exception(\"" << e.what() << "\")." << std::endl; + return 2; +} +catch ( ... ) +{ + std::cerr << std::endl + << "unknown exception." << std::endl; + return 1; +}} +