Skip to content

Commit 869930e

Browse files
committed
Improve exception handling
1 parent cce313e commit 869930e

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

Common/Constants/include/CommonConstants/make_pdg_header.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818
"""
1919

2020
import os
21-
import sys
2221
from ctypes import c_bool
2322
from enum import Enum
2423

2524
try:
2625
import ROOT # pylint: disable=import-error
2726
from ROOT import o2
2827
except (ModuleNotFoundError, ImportError) as exc:
29-
raise Exception("O2 environment is not loaded.") from exc
28+
raise OSError("O2 environment is not loaded.") from exc
3029

3130

3231
# Enum of PDG_t particles
@@ -200,13 +199,12 @@ def main():
200199
mass_root_head = "/// \\brief Declarations of masses for particles in ROOT PDG_t"
201200

202201
# Get header content before and after the generated block.
203-
print(f"File {path_header} will be updated.")
202+
print(f'File "{path_header}" will be updated.')
204203
try:
205204
with open(path_header, encoding="utf-8") as file:
206205
content_old = file.readlines()
207-
except OSError:
208-
print(f'Failed to open file "{path_header}".')
209-
sys.exit(1)
206+
except OSError as exc:
207+
raise OSError(f'Failed to open file "{path_header}".') from exc
210208
lines_header_before: list[str] = []
211209
lines_header_after: list[str] = []
212210
got_block_begin = False
@@ -264,10 +262,9 @@ def main():
264262
try:
265263
with open(path_header, "w", encoding="utf-8") as file:
266264
file.write(content_new)
267-
print(f"File {path_header} has been overwritten.")
268-
except OSError:
269-
print(f'Failed to write to file "{path_header}".')
270-
sys.exit(1)
265+
print(f'File "{path_header}" has been overwritten.')
266+
except OSError as exc:
267+
raise OSError(f'Failed to write to file "{path_header}".') from exc
271268

272269

273270
if __name__ == "__main__":

0 commit comments

Comments
 (0)