Use this as an example:
begin # "try" block puts 'I am before the raise.' raise 'An error has occurred.' # optionally: `raise Exception, "message"` puts 'I am after the raise.' # won't be executed rescue # optionally: `rescue Exception => ex` puts 'I am rescued.' ensure # will always get executed puts 'Always gets executed.' end
The equivalent code in Python would be:
try: # try block print('I am before the raise.') raise Exception('An error has occurred.') # throw an exception print('I am after the raise.') # won't be executed except: # optionally: `except Exception as ex:` print('I am rescued.') finally: # will always get executed print('Always gets executed.')
Related Posts:
- Understanding the main method of python [duplicate]
- What is the difference between rw+ and r+
- Convert bytes to a string
- How can I represent an ‘Enum’ in Python?
- Python – TypeError: ‘int’ object is not iterable
- Relative imports – ModuleNotFoundError: No module named x
- Relative imports – ModuleNotFoundError: No module named x
- How do I upgrade the Python installation in Windows 10?
- Python Variable Declaration
- Best way to convert string to bytes in Python 3?
- PermissionError: [Errno 13] Permission denied
- What is setup.py?
- Invalid character in identifier
- What is the meaning of *nix?
- Relative imports in Python 3
- Get underlined text with Markdown
- Is there a ‘foreach’ function in Python 3?
- Convert bytes to a string
- How to print like printf in Python3?
- What are type hints in Python 3.5?
- TypeError: ‘type’ object is not subscriptable when indexing in to a dictionary
- Invalid character in identifier
- ‘str’ object has no attribute ‘decode’. Python 3 error?
- Error in Python script “Expected 2D array, got 1D array instead:”?
- How to find all the indexes of a recurring item in a list?
- How do you use subprocess.check_output() in Python?
- (Help) TypeError: ‘str’ object cannot be interpreted as an integer
- How do order of operations go on Python?
- Why KeyError: 0
- Microsoft Visual C++ 14.0 is required (Unable to find vcvarsall.bat)
- builtins.TypeError: must be str, not bytes
- Pinging servers in Python
- Import error: No module name urllib2
- takes 1 positional argument but 2 were given
- Python 3 ImportError: No module named ‘ConfigParser’
- Why am I getting this syntax error: keyword can’t be an expression
- re.search().TypeError: cannot use a string pattern on a bytes-like object
- what does ? ? mean in ruby
- Add Legend to Seaborn point plot
- How can I install Python’s pip3 on my Mac?
- TypeError(“‘bool’ object is not iterable”,) when trying to return a Boolean
- Get ZeroDivisionError: float division in python
- Python 3: UnboundLocalError: local variable referenced before assignment
- How to delete last item in list?
- Don’t understand this SyntaxError: illegal target for annotation
- Using Queue in python
- TypeError(“‘bool’ object is not iterable”,) when trying to return a Boolean
- “for line in…” results in UnicodeDecodeError: ‘utf-8’ codec can’t decode byte
- SyntaxError invalid token
- ruby operator “=~” [duplicate]
- How do I format a string using a dictionary in python-3.x?
- For loop and ‘numpy.float64’ object is not iterable error
- Print multiple arguments in Python
- TypeError: cannot unpack non-iterable NoneType object
- python mpl_toolkits installation issue
- How to read lines of a file in Ruby
- Shebang doesn’t work with python3
- TypeError: ‘dict_keys’ object does not support indexing
- Ruby: undefined method `[]’ for nil:NilClass when trying to get Enumerator on an Array of Hashes
- How to strip all whitespace from string
- Python Binomial Coefficient
- pythonw.exe or python.exe?
- How to convert ‘binary string’ to normal string in Python3?
- python3 TypeError: ‘function’ object is not iterable
- How can I convert a .py to .exe for Python?
- Directing print output to a .txt file
- How to read the last line of a file in Python?
- What does “+=” (plus equals) mean?
- Converting a sentence to piglatin in Python
- Python Error – TypeError: input expected at most 1 arguments, got 3 [duplicate]
- A general tree implementation?
- Get a random sample with replacement
- Conda command is not recognized on Windows 10
- ImportError: No module named ‘django.core.urlresolvers’
- Python 3: Multiply a vector by a matrix without NumPy
- How to fix “Can’t find a default Python” error
- TypeError: ‘newline’ is an invalid keyword argument for this function
- Print empty line?
- What is the meaning of “do | |” in Ruby?
- Checking to see if a string is an integer or float
- RVM is not a function, selecting rubies with ‘rvm use …’ will not work
- How to delete specific characters from a string in Ruby?
- Ruby: What is the easiest way to remove the first element from an array?
- How to know/change current directory in Python shell?
- p vs puts in Ruby
- Fizz Buzz in Ruby for dummies
- How to embed image or picture in jupyter notebook, either from a local machine or from a web resource?
- Equivalent of “continue” in Ruby
- How to use cmp() in Python 3?
- What is the correct format to write float value to file in Python
- “E: Unable to locate package python3-pip”
- Error: No module named ‘fcntl’
- Python can’t find file
- Ruby 2.0.0p0 IRB warning: “DL is deprecated, please use Fiddle”
- Accessing dict_keys element by index in Python3
- How to understand strptime vs. strftime
- KeyError: ‘plotly_domain’
- How to break out from a ruby block?
- TypeError: ‘zip’ object is not subscriptable
- What is the canonical way to trim a string in Ruby without creating a new string?