From e1e10a92f620f928cf3867fcc178f2fc7b6da15a Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Wed, 5 Nov 2025 09:47:04 +0300 Subject: [PATCH] Run catching functions --- main.cpp | 48 +++++++++++++------------ modules/exceptions/src/exception.cppm | 32 +++++++++++++++-- modules/exceptions/src/windows.cppm | 13 +++++++ modules/threads/src/highlevel_impl.cppm | 9 +---- 4 files changed, 69 insertions(+), 33 deletions(-) diff --git a/main.cpp b/main.cpp index ce4169f..773758d 100644 --- a/main.cpp +++ b/main.cpp @@ -1,33 +1,37 @@ import std; +import ru.landgrafhomyak.BGTU.networks_1.exceptions; import ru.landgrafhomyak.BGTU.networks_1.threads; import ru.landgrafhomyak.BGTU.networks_1.sockets; int main() { - LdH::Sockets::init_sockets(); + LdH::run_catching_main([] { + LdH::Sockets::init_sockets(); - std::cout << LdH::Sockets::Address::parse("google.com", "443").to_string() << std::endl; + std::cout << LdH::Sockets::Address::parse("google.com", "443").to_string() << std::endl; + std::cout << LdH::Sockets::Address::parse("localhost123123", "443").to_string() << std::endl; - auto server = LdH::Sockets::listen_udp(LdH::Sockets::Address::parse("127.0.0.2", "8081")); + auto server = LdH::Sockets::listen_udp(LdH::Sockets::Address::parse("127.0.0.2", "8081")); - auto server_thread = LdH::fork("server", [&] { - char buffer[10]; - server.recvOneTruncating(10, buffer); - std::cout << buffer << std::endl; - server.close(); + auto server_thread = LdH::fork("server", [&] { + char buffer[10]; + server.recvOneTruncating(10, buffer); + std::cout << buffer << std::endl; + server.close(); + }); + + auto client_thread = LdH::fork("client", [&] { + auto client = LdH::Sockets::connect_udp(LdH::Sockets::Address::parse("127.0.0.2", "8081")); + char buffer[10] = "hello\n"; + client.sendOne(10, buffer); + client.close(); + }); + + server_thread.join(); + client_thread.join(); + server_thread.destroy(); + client_thread.destroy(); + + LdH::Sockets::deinit_sockets(); }); - - auto client_thread = LdH::fork("client", [&] { - auto client = LdH::Sockets::connect_udp(LdH::Sockets::Address::parse("127.0.0.2", "8081")); - char buffer[10] = "hello\n"; - client.sendOne(10, buffer); - client.close(); - }); - - server_thread.join(); - client_thread.join(); - server_thread.destroy(); - client_thread.destroy(); - - LdH::Sockets::deinit_sockets(); } diff --git a/modules/exceptions/src/exception.cppm b/modules/exceptions/src/exception.cppm index 38fb6b2..1e1b37c 100644 --- a/modules/exceptions/src/exception.cppm +++ b/modules/exceptions/src/exception.cppm @@ -9,11 +9,14 @@ namespace LdH { const std::string message; const std::vector stacktrace; - explicit inline Exception(char const *message) : message{message}, stacktrace{Exception::generateStacktrace(-1)} {} + explicit inline Exception(char const *message) : message{message}, stacktrace{Exception::generateStacktrace(-1)} { + } - explicit inline Exception(std::string const &message) : message{message}, stacktrace{Exception::generateStacktrace(-1)} {} + explicit inline Exception(std::string const &message) : message{message}, stacktrace{Exception::generateStacktrace(-1)} { + } - explicit inline Exception(std::string const &&message) : message{message}, stacktrace{Exception::generateStacktrace(-1)} {} + explicit inline Exception(std::string const &&message) : message{message}, stacktrace{Exception::generateStacktrace(-1)} { + } [[nodiscard]] static std::vector generateStacktrace(std::int_fast16_t hidden_frames_count); @@ -36,6 +39,29 @@ namespace LdH { std::cerr << "Fatal error:\n" << (Exception{msg}).formatOutput() << std::endl; std::abort(); } + + export + template + requires std::invocable && std::same_as, void> + && std::invocable && std::same_as, void> + void run_catching(prefix_printer_t const &prefix_printer, routine_t const &routine) { + try { + routine(); + } catch (LdH::Exception const &e) { + prefix_printer(); + e.printStackTrace(); + } catch (std::exception const &e) { + prefix_printer(); + std::cerr << e.what() << "\n"; + } + } + + export + template + requires std::invocable && std::same_as, void> + void run_catching_main(routine_t const &routine) { + LdH::run_catching([] { std::cerr << "Uncaught exception in main thread:\n"; }, routine); + } } module : private; diff --git a/modules/exceptions/src/windows.cppm b/modules/exceptions/src/windows.cppm index 5532d4d..6a24bea 100644 --- a/modules/exceptions/src/windows.cppm +++ b/modules/exceptions/src/windows.cppm @@ -38,8 +38,21 @@ void LdH::throwFromWindowsErrCode(DWORD code) { 0, nullptr ); + size_t trim_size = std::strlen(msg); + while (trim_size > 0) { + switch (msg[--trim_size]) { + case '\n': + case '\r': + msg[trim_size] = '\0'; + continue; + default: + break; + } + break; + } std::string msgpp{msg}; LocalFree(msg); + throw LdH::Exception{std::move(msgpp)}; } diff --git a/modules/threads/src/highlevel_impl.cppm b/modules/threads/src/highlevel_impl.cppm index e94e7c1..8656857 100644 --- a/modules/threads/src/highlevel_impl.cppm +++ b/modules/threads/src/highlevel_impl.cppm @@ -104,14 +104,7 @@ namespace LdH { thread_routine_t thread_routine = std::move(init->thread_routine); init->notifier.notify(); - try { - thread_routine(); - } catch (LdH::Exception const &e) { - std::cerr << "Uncaught exception in thread" << thread_name << ":\n"; - e.printStackTrace(); - } catch (std::exception const &e) { - std::cerr << "Uncaught exception in thread" << thread_name << ":\n" << e.what() << "\n"; - } + LdH::run_catching([&] { std::cerr << "Uncaught exception in thread" << thread_name << ":\n"; }, thread_routine); return native_wrapper_t::kernel_ret(); };