Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ganesh Pal Newsgroups: comp.lang.python Subject: sys.exit(1) vs raise SystemExit vs raise Date: Tue, 12 Apr 2016 18:20:54 +0530 Lines: 84 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de xvxB8IbPYHJDBvZVF+mciwe5Qb4DZucISbh7hJBdgbJg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '__name__': 0.07; 'main()': 0.07; 'skip:! 20': 0.07; 'oserror': 0.09; 'python': 0.10; '2.7': 0.13; 'exception': 0.13; 'def': 0.13; "'__main__':": 0.16; 'main():': 0.16; 'oserror:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sys.exit(1)': 0.16; 'systemexit': 0.16; '\xc2\xa0i': 0.16; 'try:': 0.18; 'skip:" 30': 0.20; 'to:name :python-list@python.org': 0.20; '(a)': 0.22; 'file:': 0.22; 'code.': 0.23; '(most': 0.24; 'example': 0.26; 'linux': 0.26; 'error': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; "skip:' 10": 0.28; 'fine': 0.28; 'looks': 0.29; '"': 0.29; 'raise': 0.29; 'print': 0.30; 'code': 0.30; 'subject:) ': 0.32; 'traceback': 0.33; 'definition': 0.34; 'file': 0.34; 'except': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'could': 0.35; 'replace': 0.35; 'instead': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'suggestion': 0.37; 'received:209': 0.38; 'wrong': 0.38; 'log': 0.38; 'skip:o 20': 0.38; 'to:addr:python.org': 0.40; 'skip:u 10': 0.61; 'here': 0.66 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to; bh=L8dCZRSA0vYJca/2RK/WP+SLpWsNIh9FUP850wmIIDo=; b=XfAs/Uy2/dD0aBG7OuDFrjQHi4EIUhe4HEco4I2e7RnBNFg0WM7f7b/nS43uvnPryY 7I/PLNT3aEPs4Mpl5HMNes7qB69gJC64QCgvKQa6b3lXxg5w5P7QPL2WZweqHvXUc7Gh HVYpruU7BhYknyIuqjmGqcbxXpGoi3TgJ+WQ9LfUkUo+80wtYbGluTlZE253XGOfGJYz IgYgvjds4magp6sgUtqAPRM8ekmVnWsSGKcpdFvwbNh/YLpKSp7LRHaoipNkFJ7Rz9Ti R9zBqqAENDa1my4MfPFZ+WvP49mV2+OKRdI0pfuWNB91XWH1Yb/ImzL02rvmq8BiQKgj OG9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:date:message-id:subject:from:to; bh=L8dCZRSA0vYJca/2RK/WP+SLpWsNIh9FUP850wmIIDo=; b=KBups2ZQbQMgMDoYEa9MV9RVHhVxOYaQEdvy1mhpC6+0Js9JPwWl8q1UxjYtO06TaT aS8i3xi9MeXIahkZdWqPFqslZKa1k9AcqKiUb8yZffW/9f42cJXMlxJbsRJ5OT27bLPX zJo1iH4a8iWW7m89TWPcy7Db6l+Nd/CeZaqrg7nN+cY4Er0WH1qTpE9J+7dCDgIH8T+I B9LmNFgnxwQp0W3EjmniCHFDmfR41Oh+QvmBONVJNMU34j4hlFswS1eN0MOss/p0JUCp 3fLGC2AIYuTUQgppq+uHLc3ZVVqwS9NUSzm9e+jw1B7h7KW95DPNwSN6OkL/xZNtRYXm EbIg== X-Gm-Message-State: AOPr4FWD93NdsbdIdLL+bLwD7XqvO3LZ3MX4EA6w7IPkUljMUTp/QCXdR+AyOjZaQRbBQFzxcwPRz7PtKPVBkQ== X-Received: by 10.25.20.106 with SMTP id k103mr1047046lfi.149.1460465454672; Tue, 12 Apr 2016 05:50:54 -0700 (PDT) X-Content-Filtered-By: Mailman/MimeDel 2.1.21 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: Xref: csiph.com comp.lang.python:106905 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