Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/thread/promise/set exception"

From cppreference.com
< cpp‎ | thread‎ | promise
m (s/but/therefore)
m (fmt, {{c}}, headers sorted)
 
Line 1: Line 1:
{{cpp/thread/promise/title | set_exception}}
+
{{cpp/thread/promise/title|set_exception}}
 
{{cpp/thread/promise/navbar}}
 
{{cpp/thread/promise/navbar}}
{{ddcl | since=c++11 |
+
{{ddcl|since=c++11|
 
void set_exception( std::exception_ptr p );
 
void set_exception( std::exception_ptr p );
 
}}
 
}}
  
Atomically stores the exception pointer {{tt|p}} into the shared state and makes the state ready.  
+
Atomically stores the exception pointer {{|p}} into the shared state and makes the state ready.  
  
 
The operation behaves as though {{lc|set_value}}, {{tt|set_exception}}, {{lc|set_value_at_thread_exit}}, and {{lc|set_exception_at_thread_exit}} acquire a single mutex associated with the promise object while updating the promise object.
 
The operation behaves as though {{lc|set_value}}, {{tt|set_exception}}, {{lc|set_value_at_thread_exit}}, and {{lc|set_exception_at_thread_exit}} acquire a single mutex associated with the promise object while updating the promise object.
Line 15: Line 15:
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | p | exception pointer to store. The behavior is undefined if {{tt|p}} is null. }}
+
{{par|p|exception pointer to store. The behavior is undefined if {{|p}} is null}}
 
{{par end}}
 
{{par end}}
  
Line 30: Line 30:
 
===Example===
 
===Example===
 
{{example
 
{{example
| code=
+
|code=
#include <thread>
+
#include <iostream>
+
 
#include <future>
 
#include <future>
 +
 +
  
 
int main()
 
int main()
Line 40: Line 40:
 
     std::future<int> f = p.get_future();
 
     std::future<int> f = p.get_future();
  
     std::thread t([&p]{
+
     std::thread t([&p]
         try {
+
{
 +
         try
 +
{
 
             // code that may throw
 
             // code that may throw
 
             throw std::runtime_error("Example");
 
             throw std::runtime_error("Example");
         } catch(...) {
+
         }
             try {
+
catch (...)
 +
{
 +
             try
 +
{
 
                 // store anything thrown in the promise
 
                 // store anything thrown in the promise
 
                 p.set_exception(std::current_exception());
 
                 p.set_exception(std::current_exception());
 
                 // or throw a custom exception instead
 
                 // or throw a custom exception instead
 
                 // p.set_exception(std::make_exception_ptr(MyException("mine")));
 
                 // p.set_exception(std::make_exception_ptr(MyException("mine")));
             } catch(...) {} // set_exception() may throw too
+
             }
 +
catch (...) {} // set_exception() may throw too
 
         }
 
         }
 
     });
 
     });
  
     try {
+
     try
 +
{
 
         std::cout << f.get();
 
         std::cout << f.get();
     } catch(const std::exception& e) {
+
     }
 +
catch (const std::exception& e)
 +
{
 
         std::cout << "Exception from the thread: " << e.what() << '\n';
 
         std::cout << "Exception from the thread: " << e.what() << '\n';
 
     }
 
     }
 
     t.join();
 
     t.join();
 
}
 
}
| output=
+
|output=
 
Exception from the thread: Example
 
Exception from the thread: Example
 
}}
 
}}
Line 67: Line 76:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/thread/promise/dsc set_exception_at_thread_exit}}
+
{{dsc inc|cpp/thread/promise/dsc set_exception_at_thread_exit}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 03:38, 23 October 2023