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
- SyntaxError: unexpected EOF while parsing
- How do I lowercase a string in Python?
- How do I copy a file in Python?
- How can I reverse a list in Python?
- How do I copy a file in Python?
- can’t multiply sequence by non-int of type ‘float’
- Difference between del, remove, and pop on lists
- How can I reverse a list in Python?
- How to use the pass statement
- How to use filter, map, and reduce in Python 3
- Using prevNext Modx Addon
- What does enumerate() mean?
- Searching the student-t distribution table for values using python
- How to declare an array in Python?
- :wq! command in vim
- Does Python have a ternary conditional operator?
- how to set the background color of the status bar during the launching phase [duplicate]
- Use Gif Logo For Loading Screen In Kivy
- api-ms-win-crt-runtime-l1-1-0.dll is missing when opening Microsoft Office file [closed]
- Praw & Discord.py: The bot keep sending the same meme. I want the bot to send different meme whenever it is asked
- Pig Latin Translator
- What is the difference between Python’s list methods append and extend?
- How can I make a time delay in Python? [duplicate]
- When is K 1024 and when is it 1000?
- What is a MIME type?
- Python – TypeError: ‘int’ object is not iterable
- TypeError: ‘int’ object is not subscriptable
- sphinx.ext.autodoc: Keeping names of constants in signature
- Logitech/LGHUB Lua – Loop with break
- are there dictionaries in javascript like python?
- are there dictionaries in javascript like python?
- How do you round UP a number?
- Understanding slice notation
- Iterating over dictionaries using ‘for’ loops
- How to define a two-dimensional array?
- How can I convert MP3 file to a Base64 encoded string? [closed]
- Understanding The Modulus Operator %
- how to sort pandas dataframe from one column
- Why am I seeing “TypeError: string indices must be integers”?
- Dial pad to get phone number (with Android button images)
- How do you round UP a number?
- Understanding slice notation
- How do I update\upgrade pip itself from inside my virtual environment?