![]() It's less efficient and more error prone. In Python 3: raise error.with_traceback(sys.exc_info())Īgain: avoid manually manipulating tracebacks. > catch_error_modify_message()įile "", line 3, in catch_error_modify_message Note that this is not a best practice and it is invalid syntax in Python 3 (making keeping compatibility much harder to work around). Raise error_type, error_instance, tracebackĪnd we have preserved the whole traceback while modifying the args. setting new args for the instance: def error():Įrror_type, error_instance, traceback = sys.exc_info()Įrror_instance.args = (error_instance.args + ' ',) If you want to, you can modify what happens with your new raise - e.g. Raise sys.exc_info(), sys.exc_info(), sys.exc_info() # Equivalently, as error *is* the second object: This is the syntax in Python 2 - note this is not compatible with Python 3: raise AppError, error, sys.exc_info() # avoid this. To explain - the sys.exc_info() returns the type, value, and traceback. You can preserve the stacktrace (and error value) with sys.exc_info(), but this is way more error prone and has compatibility problems between Python 2 and 3, prefer to use a bare raise to re-raise. # raise AppError # Don't do this, you'll lose the stack trace!ĭon't modify your errors. For example: logger = logging.getLogger(_name_) The best way to do this while preserving the stack trace is to use a bare raise statement. When inside an except clause, you might want to, for example, log that a specific type of error happened, and then re-raise. In Python 2.5, an actual message attribute was added to BaseException in favor of encouraging users to subclass Exceptions and stop using args, but the introduction of message and the original deprecation of args has been retracted. Some_code_that_may_raise_our_value_error() These arguments are accessed by the args attribute on the Exception object. Which also handily allows an arbitrary number of arguments to be passed to the constructor: raise ValueError('A very specific bad thing happened', 'foo', 'bar', 'baz') raise ValueError('A very specific bad thing happened') Instead, use the most specific Exception constructor that semantically fits your issue. Print('we will not catch exception: Exception')Įxception: general exceptions not caught by specific handling Raise Exception('general exceptions not caught by specific handling') Print('Caught this error: ' + repr(error))Ĭaught this error: ValueError('Represents a hidden bug, do not catch this',)Īnd more specific catches won't catch the general exception: def demo_no_catch(): Raise Exception('This is the exception you expect to handle') Raise ValueError('Represents a hidden bug, do not catch this') Problem 1: Hiding bugs raise Exception('I know Python!') # Don't! If you catch, likely to hide bugs. To catch it, you'll have to catch all other more specific exceptions that subclass it. Use the most specific Exception constructor that semantically fits your issue.īe specific in your message, e.g.: raise ValueError('A very specific bad thing happened.')Īvoid raising a generic Exception. How do I manually throw/raise an exception in Python?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |