You can but you probably shouldn’t:
try: do_something() except: print("Caught it!")
However, this will also catch exceptions like KeyboardInterrupt
and you usually don’t want that, do you? Unless you re-raise the exception right away – see the following example from the docs:
try: f = open('myfile.txt') s = f.readline() i = int(s.strip()) except IOError as (errno, strerror): print("I/O error({0}): {1}".format(errno, strerror)) except ValueError: print("Could not convert data to an integer.") except: print("Unexpected error:", sys.exc_info()[0]) raise
Related Posts:
- Manually raising (throwing) an exception in Python
- Understanding the main method of python [duplicate]
- TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array
- TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array
- Python Math – TypeError: ‘NoneType’ object is not subscriptable
- How can I safely create a nested directory in Python?
- Why does this iterative list-growing code give IndexError: list assignment index out of range?
- PermissionError: [Errno 13] in Python
- How to print an exception in Python?
- “If not” condition statement in python
- syntaxerror: “unexpected character after line continuation character in python” math
- How can I sort a dictionary by key?
- How can I safely create a nested directory in Python?
- How do I check if a variable exists?
- Python Logic of ListNode in Leetcode
- python exception message capturing
- Plot logarithmic axes with matplotlib in python
- Proper way to declare custom exceptions in modern Python?
- Error in Python script “Expected 2D array, got 1D array instead:”?
- Python Key Error=0 – Can’t find Dict error in code
- Relative imports for the billionth time
- How do I use the yfinance API in Python to get today’s DJI open?
- what does the __file__ variable mean/do?
- Pythonic way to create a long multi-line string
- Proper shebang for Python script
- ‘str’ object has no attribute ‘decode’. Python 3 error?
- Catch multiple exceptions in one line (except block)
- How do I correctly clean up a Python object?
- Log to the base 2 in python
- Implement touch using Python?
- Python setup.py develop vs install
- How to clamp an integer to some range?
- Catch multiple exceptions in one line (except block)
- How to extract the substring between two markers?
- How to properly ignore exceptions
- Is there an operation for not less than or not greater than in python?
- django MultiValueDictKeyError error, how do I deal with it
- Simple prime number generator in Python
- Background color for Tk in Python
- If Python is interpreted, what are .pyc files?
- How do I get monitor resolution in Python?
- Multiple try codes in one block
- classic ASP error ‘80020009’ Exception occurred
- What is runtime in context of Python? What does it consist of?
- Unable to convert 3d ply file image to 2d image
- simple IPython example raises exception on sys.exit()
- Python High Pass Filter
- Where are the ampersand and vertical bar characters used in Python?
- How do you test that a Python function throws an exception?
- Python: What OS am I running on?
- Handling a timeout error in python sockets
- How to draw a line with matplotlib?
- What is the maximum float in Python?
- Strange Exception in Tkinter callback
- Get all object attributes in Python?
- Normalizing a list of numbers in Python
- mport win32ui in python 3.6
- Compiler Error “void value not ignored as it ought to be” in C programming [duplicate]
- python capitalize first letter only
- Is there a short contains function for lists?
- ImportError: No module named pandas. Pandas installed pip
- How to Execute a Python Script in Notepad++?
- Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException Error
- Should I put #! (shebang) in Python scripts, and what form should it take?
- Double precision floating values in Python?
- How do I parallelize a simple Python loop?
- TypeError: ‘str’ object cannot be interpreted as an integer [duplicate]
- Updating matplotlib in virtualenv with pip
- Is the standard Python implementation considered as a programming language or a scripting language?
- Python – from . import
- Floating div positioning
- SystemError: Parent module ” not loaded, cannot perform relative import
- How to remove anaconda from windows completely?
- SyntaxError: non-default argument follows default argument
- Python Package bt: Bool index error
- Python list sort in descending order
- enumerate() for dictionary in python
- How to convert a PIL Image into a numpy array?
- Python Rule Based Engine
- upgade python version using pip
- Jenkins: 403 No valid crumb was included in the request
- Is it possible to ignore one single specific line with Pylint?
- Print list without brackets in a single row
- Get first row value of a given column
- AttributeError: ‘module’ object has no attribute
- In Python How can I declare a Dynamic Array
- Python module for converting PDF to text
- Using sudo with Python script
- What does the Ellipsis object do?
- How to concatenate items in a list to a single string?
- python pip on Windows – command ‘cl.exe’ failed
- Call python function from JS
- Rename specific column(s) in pandas
- How to create a numpy array of lists?
- How do I add two sets?
- How to count the number of files in a directory using Python
- Printing newline in MIPS
- How to avoid a System.Runtime.InteropServices.COMException?
- Why am I getting “Indicate whether to send a cookie in a cross-site request by specifying its SameSite attribute”?
- Set size of WordPress thumbnail post