Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python catch all exceptions

try:
    raise Exception("Oh no! An error happened!")
except Exception as err:
    print("An error was handled")
finally:
  	print("This runs either way.")
Comment

Catching Exceptions in python

# import module sys to get the type of exception
import sys

randomList = ['x', 0, 4]

for entry in randomList:
    try:
        print("The entry is", entry)
        r = 1/int(entry)
        break
    except:
        print(sys.exc_info()[0], "occurred.")
        print("Next entry.")
        print()
print("The reciprocal of", entry, "is", r)
Comment

python Catching Exceptions

# import module sys to get the type of exception
import sys

randomList = ['x', 0, 4]

for entry in randomList:
    try:
        print("The entry is", entry)
        r = 1/int(entry)
        break
    except Exception as e:
        print(e.__class__, "occurred.")
        print("Next entry.")
        print()
print("The reciprocal of", entry, "is", r)
Comment

PREVIOUS NEXT
Code Example
Python :: How to to efficiently find the first index in a sorted array of distinct numbers that is equal to the value at that index? 
Python :: cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first and last element of that pair. For example, car(cons(3, 4)) returns 3, and cdr(cons(3, 4)) returns 4. 
Python :: take multiple string as int in a list python 
Python :: binning dat adataframe 
Python :: a function to create a null correlation heatmap in python 
Python :: find Carmichael number sage 
Python :: get wav file in dir 
Python :: selenium python download mac 
Python :: tkinter progresse bar color 
Python :: datetime to string python 
Python :: regex python multiline 
Python :: csv python write 
Python :: python opencv create new image 
Python :: confusion matrix python 
Python :: python check if character before character in alphabet 
Python :: list to tensor 
Python :: django httpresponseredirect 
Python :: seasonal_decompose python 
Python :: python argparse 
Python :: generate valid sudoku board python 
Python :: matplotlib Savefig cuts off title 
Python :: order dataframe by multiple columns python 
Python :: random permutation python 
Python :: pandas query variable count 
Python :: how to add subplots for histogram in pandas 
Python :: managing media in django 
Python :: how to color print in python 
Python :: django genericforeignkey null 
Python :: train test validation sklearn 
Python :: sort by index pandas 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =