-
Notifications
You must be signed in to change notification settings - Fork 269
Question regarding implementation of exceptions in cpp2. #1149
-
Greetings!
Currently wondering where its possible to find a docs or examples on semantics of try {} catch {} and throw in cpp2. I've seen issues #111 and #361 but there is no explicit answer on how to define a try-catch block or how to catch what's thrown by 'throws' word i've seen in listed issues.
Thanks in advance.
Beta Was this translation helpful? Give feedback.
All reactions
Exceptions haven't yet been implemented in Cpp2/cppfront. AFAIK, there's plan for them in the future.
Replies: 2 comments 11 replies
-
Exceptions haven't yet been implemented in Cpp2/cppfront. AFAIK, there's plan for them in the future.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
If I import a cpp1 library into my cpp2 project, exceptions from my library just result in abort() and I can't retrieve the exception message.
Really annoying 😩
Beta Was this translation helpful? Give feedback.
All reactions
-
Since you're writing a library, you don't want to replace the default handler.
IIRC the way to have a contract use a specific handler is
by passing it as the first template argument,
e.g. assert<my_handler>(a && b);.
Beta Was this translation helpful? Give feedback.
All reactions
-
void test() { throw std::runtime_error("message to retrieve"); } main:() -> int = { test(); return 0; }
I can't figure out what to write in the main function to get my error message displayed at runtime.
(Assuming that the test method comes from an imported cpp1 library.)
Beta Was this translation helpful? Give feedback.
All reactions
-
Sounds like you need try/catch syntax in Cpp2, which isn't implemented yet.
Beta Was this translation helpful? Give feedback.
All reactions
-
Ah, I have the impression that this is a CMake and compiler option related problem more than a cpp2 problem. Sorry for the inconvenience and thanks for the help. I'll keep working on a solution on my own.
Beta Was this translation helpful? Give feedback.
All reactions
-
I've found the perfect simple solution, just encapsulate all the code in a try catch 😊
cpp2_main:() -> int = { // My entire cpp2 code. } int main() { try { // CPP2 workaround: Try catch not yet supported. return cpp2_main(); } catch (const std::exception& e) { std::cerr << "Caught exception: " << e.what() << std::endl; } return 0; }
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1