Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Question regarding implementation of exceptions in cpp2. #1149

Answered by DyXel
AvangardAA asked this question in Q&A
Discussion options

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.

You must be logged in to vote

Exceptions haven't yet been implemented in Cpp2/cppfront. AFAIK, there's plan for them in the future.

Replies: 2 comments 11 replies

Comment options

Exceptions haven't yet been implemented in Cpp2/cppfront. AFAIK, there's plan for them in the future.

You must be logged in to vote
0 replies
Answer selected by AvangardAA
Comment options

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 😩

You must be logged in to vote
11 replies
Comment options

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);.

Comment options

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.)

Comment options

Sounds like you need try/catch syntax in Cpp2, which isn't implemented yet.

Comment options

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.

Comment options

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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /