Clear Filters
Clear Filters

Exception handling in C++ code generated by Simulink Embedded Coder

4 views (last 30 days)
What is the recommended way to throw, catch, and handle exceptions for utility functions used in simulink models from which C++ code is then generated using Embedded Coder? I couldn't get a clear answer from the docs. The closest thing I found was this..
Which mentions something about std::runtime_error, but it is unclear if I can throw custom errors in the generated code the same way I would if I was operating inside matlab. Furthermore, try/catch are supposedly not supported for code generation, so I'm not sure how I would even handle the error if I could throw it.

Answers (1)

Varun
Varun on 28 Dec 2023
Hi Jose,
I understand that you want to throw custom errors in the generated code in the same way as it was operating inside the MATLAB.
Exception handling is unfortunately not yet supported in MATLAB/ Embedded Coder as of MATLAB R2023b.
You can refer to similar question asked on this forum, answered by the staff:
Here are some general guidelines for handling exceptions in the context of code generated from Simulink models:
  1. Use MATLAB assert and error statements within your Simulink model to check for conditions that should not occur during simulation. These can be translated into explicit error checks in the generated code.
  2. Instead of using try/catch for error handling, consider using traditional C error-handling mechanisms such as return codes or global variables to indicate errors.
  3. Implement logging mechanisms in your generated code to capture information about errors, warnings, or unexpected conditions.
Hope it helps.

Categories

Find more on Simulink Coder in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!