2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
14 _LIBCPP_SAFE_STATIC static std::terminate_handler __terminate_handler;
15 _LIBCPP_SAFE_STATIC static std::unexpected_handler __unexpected_handler;
18 // libcxxrt provides implementations of these functions itself.
20 set_unexpected(unexpected_handler func) _NOEXCEPT
22 return __libcpp_atomic_exchange(&__unexpected_handler, func);
26 get_unexpected() _NOEXCEPT
28 return __libcpp_atomic_load(&__unexpected_handler);
35 (*get_unexpected())();
36 // unexpected handler should not return
41 set_terminate(terminate_handler func) _NOEXCEPT
43 return __libcpp_atomic_exchange(&__terminate_handler, func);
47 get_terminate() _NOEXCEPT
49 return __libcpp_atomic_load(&__terminate_handler);
52 #ifndef __EMSCRIPTEN__ // We provide this in JS
57 #ifndef _LIBCPP_NO_EXCEPTIONS
60 #endif // _LIBCPP_NO_EXCEPTIONS
62 // handler should not return
63 fprintf(stderr, "terminate_handler unexpectedly returned\n");
65 #ifndef _LIBCPP_NO_EXCEPTIONS
69 // handler should not throw exception
70 fprintf(stderr, "terminate_handler unexpectedly threw an exception\n");
73 #endif // _LIBCPP_NO_EXCEPTIONS
75 #endif // !__EMSCRIPTEN__
77 #if !defined(__EMSCRIPTEN__)
78 bool uncaught_exception() _NOEXCEPT { return uncaught_exceptions() > 0; }
80 int uncaught_exceptions() _NOEXCEPT
82 #warning uncaught_exception not yet implemented
83 fprintf(stderr, "uncaught_exceptions not yet implemented\n");
86 #endif // !__EMSCRIPTEN__
89 exception::~exception() _NOEXCEPT
93 const char* exception::what() const _NOEXCEPT
95 return "std::exception";
98 bad_exception::~bad_exception() _NOEXCEPT
102 const char* bad_exception::what() const _NOEXCEPT
104 return "std::bad_exception";
108 bad_alloc::bad_alloc() _NOEXCEPT
112 bad_alloc::~bad_alloc() _NOEXCEPT
117 bad_alloc::what() const _NOEXCEPT
119 return "std::bad_alloc";
122 bad_array_new_length::bad_array_new_length() _NOEXCEPT
126 bad_array_new_length::~bad_array_new_length() _NOEXCEPT
131 bad_array_new_length::what() const _NOEXCEPT
133 return "bad_array_new_length";
136 bad_cast::bad_cast() _NOEXCEPT
140 bad_typeid::bad_typeid() _NOEXCEPT
144 bad_cast::~bad_cast() _NOEXCEPT
149 bad_cast::what() const _NOEXCEPT
151 return "std::bad_cast";
154 bad_typeid::~bad_typeid() _NOEXCEPT
159 bad_typeid::what() const _NOEXCEPT
161 return "std::bad_typeid";