Skip to main content
added 120 characters in body
Source Link
Mawg
  • 367
  • 1
  • 12

Please can you review my standard exception handler for Python 3.x. It is located under the # +=+= line. The code above the line is to create an exception and so the code is code complete and runs standalone.

The code is just a standard exception formatter, so users are presented with easier to read messages rather than stack traces. This is a common idiom in many Python projects. But many / all don't format to the extent I have.

I have searched a lot, but I can't find anything bettercan't find anything better. I expected to find something on GitHub, but was unfruitful. If you find a way to improve this, please post it here so that I and others will know.

import inspect
import os
import sys
import traceback

if __name__ == '__main__':
    try:
        x = 42/0

    # +=+=+=+=+=+=+=+=+=+=+=+=+=
    except KeyboardInterrupt as e: # Ctrl-C
        raise e
    except SystemExit as e: # sys.exit()
        raise e
    except Exception as e :
        exc_type, exc_obj, exc_tb = sys.exc_info()
        scriptName = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        functionName = inspect.currentframe().f_code.co_name
        print('=======================================================================================================')
        print('Unhandled exception: ', exc_type, scriptName, 'at line', exc_tb.tb_lineno)
        print('                      in function', functionName)
        print('                     ', str(e))

        traceback.print_exc()
        sys.exit(1)

Please can you review my standard exception handler for Python 3.x. It is located under the # +=+= line. The code above the line is to create an exception and so the code is code complete and runs standalone.

The code is just a standard exception formatter, so users are presented with easier to read messages rather than stack traces. This is a common idiom in many Python projects. But many / all don't format to the extent I have.

I have searched a lot, but I can't find anything better. I expected to find something on GitHub, but was unfruitful. If you find a way to improve this, please post it here so that I and others will know.

import inspect
import os
import sys
import traceback

if __name__ == '__main__':
    try:
        x = 42/0

    # +=+=+=+=+=+=+=+=+=+=+=+=+=
    except KeyboardInterrupt as e: # Ctrl-C
        raise e
    except SystemExit as e: # sys.exit()
        raise e
    except Exception as e :
        exc_type, exc_obj, exc_tb = sys.exc_info()
        scriptName = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        functionName = inspect.currentframe().f_code.co_name
        print('=======================================================================================================')
        print('Unhandled exception: ', exc_type, scriptName, 'at line', exc_tb.tb_lineno)
        print('                      in function', functionName)
        print('                     ', str(e))

        traceback.print_exc()
        sys.exit(1)

Please can you review my standard exception handler for Python 3.x. It is located under the # +=+= line. The code above the line is to create an exception and so the code is code complete and runs standalone.

The code is just a standard exception formatter, so users are presented with easier to read messages rather than stack traces. This is a common idiom in many Python projects. But many / all don't format to the extent I have.

I have searched a lot, but I can't find anything better. I expected to find something on GitHub, but was unfruitful. If you find a way to improve this, please post it here so that I and others will know.

import inspect
import os
import sys
import traceback

if __name__ == '__main__':
    try:
        x = 42/0

    # +=+=+=+=+=+=+=+=+=+=+=+=+=
    except KeyboardInterrupt as e: # Ctrl-C
        raise e
    except SystemExit as e: # sys.exit()
        raise e
    except Exception as e :
        exc_type, exc_obj, exc_tb = sys.exc_info()
        scriptName = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        functionName = inspect.currentframe().f_code.co_name
        print('=======================================================================================================')
        print('Unhandled exception: ', exc_type, scriptName, 'at line', exc_tb.tb_lineno)
        print('                      in function', functionName)
        print('                     ', str(e))

        traceback.print_exc()
        sys.exit(1)
Tweeted twitter.com/StackCodeReview/status/1351590297340440578
Spelling fixes
Source Link
Toby Speight
  • 88.4k
  • 14
  • 104
  • 327

Handeling Handling exceptions in Python 3

Please can you review my standard exception handler for Python 3.x. It is located under the # +=+= line. The code above the line is to create an exception and so the code is code complete and runs standalone.

The code is just a standard exception formatter, so users are presented with easier to read messages rather than stack traces. This is a common idiom in many Python projects. But many / all don't format to the extent I have.

I have searchsearched a lot, but I can't find anything better. I expected to find something on GitHub, but was unfruitful. If you find a way to improve this, please post it here so that I and others will know.

import inspect
import os
import sys
import traceback

if __name__ == '__main__':
    try:
        x = 42/0

    # +=+=+=+=+=+=+=+=+=+=+=+=+=
    except KeyboardInterrupt as e: # Ctrl-C
        raise e
    except SystemExit as e: # sys.exit()
        raise e
    except Exception as e :
        exc_type, exc_obj, exc_tb = sys.exc_info()
        scriptName = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        functionName = inspect.currentframe().f_code.co_name
        print('=======================================================================================================')
        print('Unhandled exception: ', exc_type, scriptName, 'at line', exc_tb.tb_lineno)
        print('                      in function', functionName)
        print('                     ', str(e))

        traceback.print_exc()
        sys.exit(1)

Handeling exceptions in Python 3

Please can you review my standard exception handler for Python 3.x. It is located under the # +=+= line. The code above the line is to create an exception and so the code is code complete and runs standalone.

The code is just a standard exception formatter, so users are presented easier to read messages rather than stack traces. This is a common idiom in many Python projects. But many / all don't format to the extent I have.

I have search a lot but I can't find anything better. I expected to find something on GitHub, but was unfruitful. If you find a way to improve this please post it here so that I and others will know.

import inspect
import os
import sys
import traceback

if __name__ == '__main__':
    try:
        x = 42/0

    # +=+=+=+=+=+=+=+=+=+=+=+=+=
    except KeyboardInterrupt as e: # Ctrl-C
        raise e
    except SystemExit as e: # sys.exit()
        raise e
    except Exception as e :
        exc_type, exc_obj, exc_tb = sys.exc_info()
        scriptName = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        functionName = inspect.currentframe().f_code.co_name
        print('=======================================================================================================')
        print('Unhandled exception: ', exc_type, scriptName, 'at line', exc_tb.tb_lineno)
        print('                      in function', functionName)
        print('                     ', str(e))

        traceback.print_exc()
        sys.exit(1)

Handling exceptions in Python 3

Please can you review my standard exception handler for Python 3.x. It is located under the # +=+= line. The code above the line is to create an exception and so the code is code complete and runs standalone.

The code is just a standard exception formatter, so users are presented with easier to read messages rather than stack traces. This is a common idiom in many Python projects. But many / all don't format to the extent I have.

I have searched a lot, but I can't find anything better. I expected to find something on GitHub, but was unfruitful. If you find a way to improve this, please post it here so that I and others will know.

import inspect
import os
import sys
import traceback

if __name__ == '__main__':
    try:
        x = 42/0

    # +=+=+=+=+=+=+=+=+=+=+=+=+=
    except KeyboardInterrupt as e: # Ctrl-C
        raise e
    except SystemExit as e: # sys.exit()
        raise e
    except Exception as e :
        exc_type, exc_obj, exc_tb = sys.exc_info()
        scriptName = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        functionName = inspect.currentframe().f_code.co_name
        print('=======================================================================================================')
        print('Unhandled exception: ', exc_type, scriptName, 'at line', exc_tb.tb_lineno)
        print('                      in function', functionName)
        print('                     ', str(e))

        traceback.print_exc()
        sys.exit(1)
Add context from the comments to the question. Reorganize content and add a little more explination.
Source Link
Peilonrayz
  • 44.6k
  • 7
  • 80
  • 158

Handeling exceptions in Python exception handler3

Here's a program to throw an exception, plusPlease can you review my standard exception handler (forfor Python 3.x). It is located under the # +=+= line. The code above the line is to create an exception and so the code is code complete and runs standalone.

The code is just a standard exception formatter, which I would likeso users are presented easier to read messages rather than stack traces. This is a common idiom in many Python projects. But many / all don't format to the extent I have reviewed.

I have search a lot but I can't find anything better. I expected to find something on GitHub, but was unfruitful. If you find a way to improve this please post it here so that I and others will know.

import inspect
import os
import sys
import traceback

if __name__ == '__main__':
    try:
        x = 42/0

    # +=+=+=+=+=+=+=+=+=+=+=+=+=
    except KeyboardInterrupt as e: # Ctrl-C
        raise e
    except SystemExit as e: # sys.exit()
        raise e
    except Exception as e :
        exc_type, exc_obj, exc_tb = sys.exc_info()
        scriptName = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        functionName = inspect.currentframe().f_code.co_name
        print('=======================================================================================================')
        print('Unhandled exception: ', exc_type, scriptName, 'at line', exc_tb.tb_lineno)
        print('                      in function', functionName)
        print('                     ', str(e))

        traceback.print_exc()
        sys.exit(1)

In fact, in a recent tweak, I changed that sys.exit(1) to a call to a function which does so, so that I only need to breakpoint that fucntion, not every exception handler.

Python exception handler

Here's a program to throw an exception, plus my standard exception handler (for Python 3.x), which I would like to have reviewed.

if __name__ == '__main__':
    try:
        x = 42/0

    # +=+=+=+=+=+=+=+=+=+=+=+=+=
    except KeyboardInterrupt as e: # Ctrl-C
        raise e
    except SystemExit as e: # sys.exit()
        raise e
    except Exception as e :
        exc_type, exc_obj, exc_tb = sys.exc_info()
        scriptName = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        functionName = inspect.currentframe().f_code.co_name
        print('=======================================================================================================')
        print('Unhandled exception: ', exc_type, scriptName, 'at line', exc_tb.tb_lineno)
        print('                      in function', functionName)
        print('                     ', str(e))

        traceback.print_exc()
        sys.exit(1)

In fact, in a recent tweak, I changed that sys.exit(1) to a call to a function which does so, so that I only need to breakpoint that fucntion, not every exception handler.

Handeling exceptions in Python 3

Please can you review my standard exception handler for Python 3.x. It is located under the # +=+= line. The code above the line is to create an exception and so the code is code complete and runs standalone.

The code is just a standard exception formatter, so users are presented easier to read messages rather than stack traces. This is a common idiom in many Python projects. But many / all don't format to the extent I have.

I have search a lot but I can't find anything better. I expected to find something on GitHub, but was unfruitful. If you find a way to improve this please post it here so that I and others will know.

import inspect
import os
import sys
import traceback

if __name__ == '__main__':
    try:
        x = 42/0

    # +=+=+=+=+=+=+=+=+=+=+=+=+=
    except KeyboardInterrupt as e: # Ctrl-C
        raise e
    except SystemExit as e: # sys.exit()
        raise e
    except Exception as e :
        exc_type, exc_obj, exc_tb = sys.exc_info()
        scriptName = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        functionName = inspect.currentframe().f_code.co_name
        print('=======================================================================================================')
        print('Unhandled exception: ', exc_type, scriptName, 'at line', exc_tb.tb_lineno)
        print('                      in function', functionName)
        print('                     ', str(e))

        traceback.print_exc()
        sys.exit(1)
added 427 characters in body
Source Link
Mawg
  • 367
  • 1
  • 12
Loading
added 53 characters in body; edited tags
Source Link
Mast
  • 13.8k
  • 12
  • 57
  • 127
Loading
Source Link
Mawg
  • 367
  • 1
  • 12
Loading