Skip to content
Advertisement

An implicit try {} catch around main

In each of my main functions, I would like to catch certain classes of exceptions and convert them to exit codes.

Is there a more elegant solution to this than starting and ending each main function with macros that will paste the implicit try {} catch I want?

Can I somehow achieve this with the std::set_terminate functionality?

Example:

JavaScript

Advertisement

Answer

A clean way involves using a translation function with all of your exception boilerplate that returns the exit values for the corresponding exception.

JavaScript

In your own code, you only concern yourself with wrapping your business logic with a lambda and passed that into the translation function so it can capture and translate for you.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement