About 128,000 results
Open links in new tab
  1. python - How can I catch multiple exceptions in one line? (in the ...

    As of Python 3.11 you can take advantage of the except* clause that is used to handle multiple exceptions. PEP-654 introduced a new standard exception type called ExceptionGroup that …

  2. Best Practices for Python Exceptions? - Stack Overflow

    Robust exception handling (in Python) - a "best practices for Python exceptions" blog post I wrote a while ago. You may find it useful. Some key points from the blog: Never use exceptions for flow …

  3. Catching an exception while using a Python 'with' statement

    The best way to handle exceptions in python is to write a function that catches and returns them? Seriously? The pythonic way to handle exceptions is to use a try...except statement.

  4. python - When I catch an exception, how do I get the type, file, and ...

    56 Source (Py v2.7.3) for traceback.format_exception () and called/related functions helps greatly. Embarrassingly, I always forget to Read the Source. I only did so for this after searching for similar …

  5. Why do we need the "finally" clause in Python? - Stack Overflow

    Feb 7, 2017 · The finally clause is executed in any event before leaving the try statement, whether an exception (even if you do not handle it) has occurred or not. I second @Byers's example.

  6. python - Difference between except: and except Exception as e: - Stack ...

    Apr 6, 2021 · The one you linked to asks what's the difference between except Exception, e: and except Exception as e:. This question asks what the difference is between except: and except Exception as e:.

  7. python - Catch a thread's exception in the caller thread ... - Stack ...

    Mar 28, 2022 · If you are using six or on Python 3 only, you can improve the stack trace information you get when the exception is re-raised. Instead of only the stack at the point of the join, you can wrap …

  8. python exception handling - Stack Overflow

    Jan 4, 2011 · import logging logging.basicConfig(level=logging.DEBUG) logging.debug('This message should go to the log file') try: 1/0 except Exception as e: logging.exception(e) This example uses the …

  9. Correct way of handling exceptions in Python? - Stack Overflow

    Aug 17, 2009 · If each line of your program can throw several different exceptions, and each needs to be handled individually, then the bulk of your code is going to be exception handling.

  10. How do I declare custom exceptions in modern Python?

    How do I declare custom exception classes in modern Python? My primary goal is to follow whatever standard other exception classes have, so that (for instance) any extra string I include in the …