Author Topic: Modeling C++ exceptions  (Read 23705 times)

amacara1

  • EA User
  • **
  • Posts: 50
  • Karma: +0/-0
    • View Profile
Modeling C++ exceptions
« on: November 09, 2023, 03:21:45 am »
I thought of using Events to model C++ exceptions: an interruptible region (corresponding to C++ try) would have an "unlinked" receive event in it (corresponding to C++ catch) that out-flows into catch-body activities. BUT, events are added as links to a diagram and thus a diagram cannot hold multiple instances of a (same) event. So, different catches cannot be modeled.
I then thought of using Signal, but I cannot really differentiate between "sent"/"received" Signals (thrown/caught).

Do you have an example of how to model C++ exceptions, please?

PDC

  • EA User
  • **
  • Posts: 90
  • Karma: +4/-0
  • Systems Engineer
    • View Profile
Re: Modeling C++ exceptions
« Reply #1 on: January 10, 2024, 09:59:21 pm »
If your Exception ('catch' Event) can occur at any point during the Activity, then can't you just place the entire Actvity (or as much as is required) within one InterruptibleRegion that catches that same Event? This would work as long as the Exception is in no way dependent on invoking your Try calls.
Phil

steubeat

  • EA Novice
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Re: Modeling C++ exceptions
« Reply #2 on: February 22, 2024, 01:17:20 pm »
You're right, directly modeling C++ exceptions with Events or Signals can be challenging due to their limitations. Here are some alternative approaches you can consider:

1. State Chart with Entry/Exit Actions:
  • Use a state chart to represent the normal execution flow and exceptional states.
  • Define entry and exit actions for states and transitions.
  • In the interruptible region (corresponding to the C++ try block), the entry action can check for exceptional conditions and trigger a transition to an "exception handling" state.
  • Define multiple exit actions for the "exception handling" state, each corresponding to a different C++ catch block. These exit actions would represent the catch-body activities.
  • This approach avoids the limitation of multiple instances of the same event by using state transitions and actions instead.
2. Nested Activities with Error Handling:
  • Use nested activities to represent the different code blocks within the try block and potential catch blocks.
  • Define an additional "error handling" activity that can be invoked from multiple locations within the nested activities.
  • Pass information about the type of exception to the "error handling" activity to differentiate between different catch blocks.
  • This approach leverages the hierarchical structure of activities to model the nesting of code blocks and the centralized handling of exceptions.
3. Custom Extension:
  • If your modeling tool allows custom extensions, create a new element specifically for modeling C++ exceptions.
  • This element could encapsulate the try, throw, and catch keywords and have internal logic to handle different exception types and corresponding actions.
  • This approach provides the most direct representation of C++ exceptions but requires specific customization to your modeling tool.
Additional Tips:
  • Regardless of the approach you choose, clearly document the mapping between your chosen modeling elements and the corresponding C++ constructs.
  • Use labels and annotations to explain the logic behind exception handling behavior within your model.
  • Consider the maintainability and scalability of your approach, especially if you have a complex model with many potential exceptions.
Remember, the best approach depends on your specific modeling tool, the complexity of your C++ code, and your personal preferences. Evaluate each option and choose the one that best suits your needs. Source: fnaf games series