xtd 0.2.0
Loading...
Searching...
No Matches
exception_ptr.cpp

Shows how to use std::exception_ptr and invalid_operation_exception exception.

#include <xtd/threading/thread>
#include <xtd/console>
#include <xtd/invalid_operation_exception>
using namespace xtd;
using namespace xtd::threading;
auto main()->int {
auto exception_pointer = std::exception_ptr {};
auto thread1 = thread {[&] {
try {
throw invalid_operation_exception("Ouch there are an exception !", csf_);
} catch (...) {
exception_pointer = std::current_exception();
}
}};
thread1.start();
thread1.join();
if (exception_pointer) {
try {
rethrow_exception(exception_pointer);
} catch (const system_exception& e) {
console::write_line(e);
}
}
}
// This code can produces the following output :
//
// xtd::invalid_operation_exception : Ouch there are an exception !
// at main::$_0::operator()() const [0x0000C1F0] in /|---OMITTED---|/exception_ptr/src/exception_ptr.cpp:line 13
// at decltype(std::__1::forward<main::$_0>(fp)()) std::__1::__invoke<main::$_0>(main::$_0&&) [0x0000C180] in /|---OMITTED---|/exception_ptr/Debug/exception_ptr:line 0
// at void std::__1::__thread_execute<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, main::$_0>(std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, main::$_0>&, std::__1::__tuple_indices<>) [0x0000C0E0] in /|---OMITTED---|/exception_ptr/Debug/exception_ptr:line 0
// at void* std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, main::$_0> >(void*) [0x0000B8E0] in /|---OMITTED---|/exception_ptr/Debug/exception_ptr:line 0
// at _pthread_start [0x000060D8] in /usr/lib/system/introspection/libsystem_pthread.dylib:line 0
The exception that is thrown when a method call is invalid for the object's current state.
Definition invalid_operation_exception.h:18
Defines the base class for predefined exceptions in the xtd namespace.
Definition system_exception.h:25
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.h:41
#define csf_
Provides information about the current stack frame.
Definition current_stack_frame.h:30
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.h:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10