Skip to content

Commit f7a3335

Browse files
committed
Improve exception handling in Driver class
Replaces OSError with Exception in several try-except blocks to catch a broader range of errors during file operations and subprocess calls. Adds comments to clarify that file removal errors are ignored if the file does not exist. Signed-off-by: shbhmexe <[email protected]>
1 parent d8495bf commit f7a3335

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

SU2_PY/topology_optimization.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def obj_val(self, x):
124124
try:
125125
os.remove(self._objValFile)
126126
except OSError:
127-
pass
127+
pass # Ignore error if file does not exist
128128

129129
try:
130130
sp.call(self._objValCommand, shell=True)
@@ -136,7 +136,7 @@ def obj_val(self, x):
136136
break
137137
# the return code of mpirun is useless, we test the value of the function
138138
self._assert_isfinite(val)
139-
except OSError:
139+
except Exception:
140140
raise RuntimeError("Objective function evaluation failed")
141141
# end
142142

@@ -151,7 +151,7 @@ def obj_der(self, x):
151151
try:
152152
os.remove(self._objDerFile)
153153
except OSError:
154-
pass
154+
pass # Ignore error if file does not exist
155155
N = x.shape[0]
156156
y = np.ndarray((N,))
157157

@@ -167,7 +167,7 @@ def obj_der(self, x):
167167
self._assert_isfinite(val)
168168
y[i] = val * obj_scale / var_scale
169169
# end
170-
except OSError:
170+
except Exception:
171171
raise RuntimeError("Objective gradient evaluation failed")
172172
# end
173173

@@ -186,7 +186,7 @@ def con_val(self, x):
186186
val = float(lines[1].split(",")[col])
187187
break
188188
self._assert_isfinite(val)
189-
except OSError:
189+
except Exception:
190190
raise RuntimeError("Constraint function evaluation failed")
191191
# end
192192

@@ -201,7 +201,7 @@ def con_der(self, x):
201201
try:
202202
os.remove(self._conDerFile)
203203
except OSError:
204-
pass
204+
pass # Ignore error if file does not exist
205205
N = x.shape[0]
206206
y = np.ndarray((N,))
207207

@@ -217,7 +217,7 @@ def con_der(self, x):
217217
self._assert_isfinite(val)
218218
y[i] = val * con_scale / var_scale
219219
# end
220-
except OSError:
220+
except Exception:
221221
raise RuntimeError("Constraint function evaluation failed")
222222
# end
223223

0 commit comments

Comments
 (0)