Skip to content

Conversation

@ondras12345
Copy link
Contributor

This closes #260

I chose to use KeyError over ModelicaSystemError, because it makes the implementation easier (dicts already raise it for us), and because I find it useful to be able to distinguish invalid input from other errors. If you prefer ModelicaSystemError, let me know and I'll change it.

@syntron
Copy link
Contributor

syntron commented Jun 11, 2025

@ondras12345 I tried to use project specific exceptions if possible (and meaningful); thus, on an exception I could catch these to ensure there is no project related error.

My changes to the code are based on this assumption; i.e. raise ModelicaSystemError in ModelicaSystem.py and OMCSessionException in OMCSession.py if the case is due to OMC or the related Python code.

@ondras12345
Copy link
Contributor Author

ondras12345 commented Jun 11, 2025

on an exception I could catch these to ensure there is no project related error

@syntron I'm not sure I understand what you mean by this. Could you please write a short demo?

In my mind, if a user writes mod.getParameters("badParameter"), it isn't much different from doing {}["badKey"] in his own code.
KeyError has a clear meaning: you tried to get a value that does not exist in the collection.
ModelicaSystemError could mean many different things (model failed to simulate due to running out of RAM, etc.).
Raising ModelicaSystemError feels like raising bare Exception instead of KeyError.

A similar situation in numpy:

In [1]: import numpy as np
In [2]: A = np.array([[1,2],[3,4]])
In [3]: A[1,2]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[3], line 1
----> 1 A[1,2]

IndexError: index 2 is out of bounds for axis 1 with size 2

It raises a standard IndexError rather than a custom exception.

If you want to make sure that the exception you are catching came from OMPython, you should probably only put the commands that use OMPython in the try ... except block, and keep your code out of it.

However, this is all just my opinion, and I'm open to changing it if you show me a convincing use case / logical reason.

... instead of returning weird values like "NotExist"
When IOError constructor is called with two arguments, the first
argument is interpreted as the error number:
In [1]: str(IOError("/test1", "does not exist"))
Out[1]: '[Errno /test1] does not exist'
@syntron
Copy link
Contributor

syntron commented Jun 16, 2025

@ondras12345 I'm fine with your approach!

My way is focused on the possibility to catch all errors related to ModelicaSystem by this one exception; this would mean that a check for this would ensure that the functionality of ModelicaSystem is covert.

@adeas31 adeas31 merged commit f16a61f into OpenModelica:master Jun 17, 2025
5 checks passed
@adeas31 adeas31 added this to the 4.0.0 milestone Jun 17, 2025
@syntron syntron mentioned this pull request Sep 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent getter behavior when requested value does not exist

3 participants