Skip to content

Commit 5f13729

Browse files
committed
[ModelicaSystem] exception handling
1 parent 10b90dd commit 5f13729

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def __init__(
158158
mod = ModelicaSystem("ModelicaModel.mo", "modelName", [("Modelica","3.2.3"), "PowerSystems"])
159159
"""
160160
if fileName is None and modelName is None and not lmodel: # all None
161-
raise Exception("Cannot create ModelicaSystem object without any arguments")
161+
raise ModelicaSystemError("Cannot create ModelicaSystem object without any arguments")
162162

163163
self.quantitiesList = []
164164
self.paramlist = {}
@@ -205,7 +205,7 @@ def __init__(
205205
self._raiseerrors = raiseerrors
206206

207207
if self.fileName is not None and not self.fileName.is_file(): # if file does not exist
208-
raise IOError(f"File Error: {self.fileName} does not exist!!!")
208+
raise IOError(f"{self.fileName} does not exist!")
209209

210210
# set default command Line Options for linearization as
211211
# linearize() will use the simulation executable and runtime
@@ -313,8 +313,8 @@ def _run_cmd(self, cmd: list, timeout: Optional[int] = None):
313313
logger.info("OM output for command %s:\n%s", cmd, stdout)
314314
except subprocess.TimeoutExpired:
315315
raise ModelicaSystemError(f"Timeout running command {repr(cmd)}")
316-
except Exception as e:
317-
raise ModelicaSystemError(f"Exception {type(e)} running command {cmd}: {e}")
316+
except Exception as ex:
317+
raise ModelicaSystemError(f"Error running command {cmd}") from ex
318318

319319
def _raise_error(self, errstr: str):
320320
if self._raiseerrors:
@@ -437,8 +437,8 @@ def getContinuous(self, names=None): # 4
437437
try:
438438
value = self.getSolutions(i)
439439
self.continuouslist[i] = value[0][-1]
440-
except Exception:
441-
raise ModelicaSystemError(f"OM error: {i} could not be computed")
440+
except OMCSessionException as ex:
441+
raise ModelicaSystemError(f"{i} could not be computed") from ex
442442
return self.continuouslist
443443

444444
elif isinstance(names, str):
@@ -447,7 +447,7 @@ def getContinuous(self, names=None): # 4
447447
self.continuouslist[names] = value[0][-1]
448448
return [self.continuouslist.get(names)]
449449
else:
450-
raise ModelicaSystemError(f"OM error: {names} is not continuous")
450+
raise ModelicaSystemError(f"{names} is not continuous")
451451

452452
elif isinstance(names, list):
453453
valuelist = []
@@ -457,7 +457,7 @@ def getContinuous(self, names=None): # 4
457457
self.continuouslist[i] = value[0][-1]
458458
valuelist.append(value[0][-1])
459459
else:
460-
raise ModelicaSystemError(f"OM error: {i} is not continuous")
460+
raise ModelicaSystemError(f"{i} is not continuous")
461461
return valuelist
462462

463463
raise ModelicaSystemError("Unhandled input for getContinous()")
@@ -717,7 +717,7 @@ def simulate(self, resultfile=None, simflags=None, timeout: Optional[int] = None
717717

718718
exe_file = self.get_exe_file()
719719
if not exe_file.exists():
720-
raise Exception(f"Error: Application file path not found: {exe_file}")
720+
raise ModelicaSystemError(f"Application file path not found: {exe_file}")
721721

722722
cmd = exe_file.as_posix() + override + csvinput + resultfileflag + simflags
723723
cmd = [s for s in cmd.split(' ') if s]
@@ -1099,7 +1099,7 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
10991099
simflags = " " + simflags
11001100

11011101
if not exe_file.exists():
1102-
raise Exception(f"Error: Application file path not found: {exe_file}")
1102+
raise ModelicaSystemError(f"Application file path not found: {exe_file}")
11031103
else:
11041104
cmd = exe_file.as_posix() + linruntime + override + csvinput + simflags
11051105
cmd = [s for s in cmd.split(' ') if s]
@@ -1129,8 +1129,8 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
11291129
self.linearstates = stateVars
11301130
return LinearizationResult(n, m, p, A, B, C, D, x0, u0, stateVars,
11311131
inputVars, outputVars)
1132-
except ModuleNotFoundError:
1133-
raise Exception("ModuleNotFoundError: No module named 'linearized_model'")
1132+
except ModuleNotFoundError as ex:
1133+
raise ModelicaSystemError("No module named 'linearized_model'") from ex
11341134

11351135
def getLinearInputs(self):
11361136
"""

0 commit comments

Comments
 (0)