🌐
W3Schools
w3schools.com › python › python_try_except.asp
Python Try Except
These exceptions can be handled ... an error, the except block will be executed. Without the try block, the program will crash and raise an error:...
🌐
Python documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.5rc1 documentation
The except clause may specify a variable after the exception name. The variable is bound to the exception instance which typically has an args attribute that stores the arguments. For convenience, builtin exception types define __str__() to print all the arguments without explicitly accessing .args. >>> try: ...
🌐
W3Schools
w3schoolsua.github.io › python › python_try_except_en.html
Python Try Except. Lessons for beginners. W3Schools in English
These exceptions can be handled ... an error, the except block will be executed. Without the try block, the program will crash and raise an error:...
🌐
W3Schools
w3schools.com › python › ref_keyword_except.asp
Python except Keyword
x = 1 try: x > 10 except NameError: print("You have a variable that is not defined.") except TypeError: print("You are comparing values of different type") else: print("The 'Try' code was executed without raising any errors!") Try it Yourself » · The try keyword. The finally keyword. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
W3Schools
w3schools.com › python › python_challenges_try_except.asp
Python Try...Except Code Challenge
Test your understanding of Python try...except by completing a small coding challenge.
🌐
W3Schools
w3schools.com › python › gloss_python_error_handling.asp
Python Error Handling
These exceptions can be handled ... an error, the except block will be executed. Without the try block, the program will crash and raise an error:...
🌐
W3Schools
w3schools.com › python › gloss_python_try_else.asp
Python Try Else
try: print("Hello") except: print("Something went wrong") else: print("Nothing went wrong") Try it Yourself » · Python Try Except Tutorial Error Handling Handle Many Exceptions Try Finally raise
🌐
W3Schools
w3schools.com › python › exercise.asp
Exercise: - Python Try Except
I completed one of the Python exercises on w3schools.com
🌐
W3Schools
w3schools.in › python › exception-handling
Python Exceptions Handling - W3Schools
Error handling: The exceptions get raised whenever Python detects an error in a program at runtime. As a programmer, if you don't want the default behavior, then code a 'try' statement to catch and recover the program from an exception.
Find elsewhere
Top answer
1 of 12
1242
try:
    doSomething()
except Exception: 
    pass

or

try:
    doSomething()
except: 
    pass

The difference is that the second one will also catch KeyboardInterrupt, SystemExit and stuff like that, which are derived directly from BaseException, not Exception.

See documentation for details:

  • try statement
  • exceptions

However, it is generally bad practice to catch every error - see Why is "except: pass" a bad programming practice?

2 of 12
167

It's generally considered best-practice to only catch the errors you are interested in. In the case of shutil.rmtree it's probably OSError:

>>> shutil.rmtree("/fake/dir")
Traceback (most recent call last):
    [...]
OSError: [Errno 2] No such file or directory: '/fake/dir'

If you want to silently ignore that error, you would do:

try:
    shutil.rmtree(path)
except OSError:
    pass

Why? Say you (somehow) accidently pass the function an integer instead of a string, like:

shutil.rmtree(2)

It will give the error "TypeError: coercing to Unicode: need string or buffer, int found" - you probably don't want to ignore that, which can be difficult to debug.

If you definitely want to ignore all errors, catch Exception rather than a bare except: statement. Again, why?

Not specifying an exception catches every exception, including the SystemExit exception which for example sys.exit() uses:

>>> try:
...     sys.exit(1)
... except:
...     pass
... 
>>>

Compare this to the following, which correctly exits:

>>> try:
...     sys.exit(1)
... except Exception:
...     pass
... 
shell:~$ 

If you want to write ever better behaving code, the OSError exception can represent various errors, but in the example above we only want to ignore Errno 2, so we could be even more specific:

import errno

try:
    shutil.rmtree(path)
except OSError as e:
    if e.errno != errno.ENOENT:
        # ignore "No such file or directory", but re-raise other errors
        raise
🌐
W3Schools
w3schools.com › python › gloss_python_try_finally.asp
Python Try Finally
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
🌐
Python Basics
pythonbasics.org › home › python basics › try and except in python
Try and Except in Python - pythonbasics.org
The try except statement can handle exceptions. Exceptions may happen when you run a program. Exceptions are errors that happen during execution of the program. Python won't tell you about errors like syntax errors (grammar faults), instead it will abruptly stop.
🌐
W3Schools
w3schools.com › python › ref_keyword_try.asp
Python try Keyword
try: x > 3 except: Exception("Something went wrong") Try it Yourself » · The except keyword. The finally keyword. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
GeeksforGeeks
geeksforgeeks.org › python-try-except
Python Try Except - GeeksforGeeks
March 19, 2025 - # Python code to illustrate # working of try() def divide(x, y): try: # Floor Division : Gives only Fractional Part as Answer result = x // y print("Yeah ! Your answer is :", result) except ZeroDivisionError: print("Sorry ! You are dividing by zero ") # Look at parameters and note the working of Program divide(3, 0) ... Example #3: The other way of writing except statement, is shown below and in this way, it only accepts exceptions that you're meant to catch or you can check which error is occurring.
🌐
Real Python
realpython.com › python-exceptions
Python Exceptions: An Introduction – Real Python
December 1, 2024 - It’s bad practice to catch all exceptions at once using except Exception or the bare except clause. Combining try, except, and pass allows your program to continue silently without handling the exception.
🌐
Python.org
discuss.python.org › python help
[SOLVED]How to catch error signals without try / except - Python Help - Discussions on Python.org
December 19, 2021 - Hi, First time posting here. I’d like to catch any error signal produced, be it a NameError, IndentationError, or whatnot. I don’t care to escape them, I would just like to be able to call some simple function whenever such an error occurs. Some people might say that this is a bad idea, ...
🌐
Python Land
python.land › home › language deep dives › python try except: examples and best practices
Python Try Except: Examples And Best Practices • Python Land Tutorial
January 29, 2026 - Learn Python exception handling with Python's try and except keywords. You'll also learn to create custom exceptions.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Try, except, else, finally in Python (Exception handling) | note.nkmk.me
August 15, 2023 - However, it's often preferable not to catch these particular exceptions. In such cases, using Exception instead may be a better option, as described next. You can specify Exception in the except clause, which is the base class for all built-in, non-system-exiting exceptions. Built-in Exceptions - Exception — Python 3.11.3 documentation · def divide_exception(a, b): try: print(a / b) except Exception as e: print(e) divide_exception(1, 0) # division by zero divide_exception('a', 'b') # unsupported operand type(s) for /: 'str' and 'str'
🌐
Cach3
w3schools.com.cach3.com › python › gloss_python_error_handling.asp.html
Python Error Handling - W3Schools
These exceptions can be handled ... · Since the try block raises an error, the except block will be executed. Without the try block, the program will crash and raise an error:...
🌐
Medium
medium.com › @rafidghadah › pythons-try-except-statement-bringing-the-fun-to-error-handling-fce8920cc02
Pythons Try Except Statement: Bringing The Fun to Error Handling! | by Rafidghadah Damarta | Medium
September 21, 2023 - To address this exception, we use the except block to catch it. We assign the error message “Oops! That was not an integer. Try again…” to the result variable to provide feedback to the user. Preventing Unwanted Termination: Without error handling, a single exception could lead to the termination of your entire program.