Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #106905
| From | Ganesh Pal <ganesh1pal@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | sys.exit(1) vs raise SystemExit vs raise |
| Date | 2016-04-12 18:20 +0530 |
| Message-ID | <mailman.44.1460465456.15650.python-list@python.org> (permalink) |
| References | <CACT3xuUFeOXbjwiWhgZ90UMPi5xXQaj9HpxU3Aepz2Hr8Kw42Q@mail.gmail.com> |
I m on python 2.7 and Linux , I have a simple code need suggestion if I
I could replace sys.exit(1) with raise SystemExit .
==Actual code==
def main():
try:
create_logdir()
create_dataset()
unittest.main()
except Exception as e:
logging.exception(e)
sys.exit(EXIT_STATUS_ERROR)
if __name__ == '__main__':
main()
==Changed Code==
def main():
try:
create_logdir()
create_dataset()
unittest.main()
except Exception as e:
logging.exception(e)
raise SystemExit
if __name__ == '__main__':
main()
2. All the functions in try block have exception bubbled out using raise
Example for create_logdir() here is the function definition
def create_logdir():
try:
os.makedirs(LOG_DIR)
except OSError as e:
sys.stderr.write("Failed to create log directory...Exiting !!!")
raise
print "log file: " + corrupt_log
return True
def main():
try:
create_logdir()
except Exception as e:
logging.exception(e)
raise SystemExit
(a) In case if create_logdir() fails we will get the below error ,is
this fine or do I need to improve this code.
Failed to create log directory...Exiting !!!ERROR:root:[Errno 17] File
exists: '/var/log/dummy'
Traceback (most recent call last):
File "corrupt_test.py", line 245, in main
create_logdir()
File "corrupt_test.py", line 53, in create_logdir
os.makedirs(LOG_DIR)
File "/usr/local/lib/python2.7/os.py", line 157, in makedirs
OSError: [Errno 17] File exists: '/var/log/dummy'
3. Can I have just raise , instead of SystemExit or sys.exit(1) . This
looks wrong to me
def main():
try:
create_logdir()
except Exception as e
logging.exception(e)
raise
Regards,
Ganesh
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
sys.exit(1) vs raise SystemExit vs raise Ganesh Pal <ganesh1pal@gmail.com> - 2016-04-12 18:20 +0530
csiph-web