Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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

Catching Specific Exceptions in Python

try:
   # do something
   pass

except ValueError:
   # handle ValueError exception
   pass

except (TypeError, ZeroDivisionError):
   # handle multiple exceptions
   # TypeError and ZeroDivisionError
   pass

except:
   # handle all other exceptions
   pass
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 :: Improve the Request Handle Errors 
Python :: python insert text in string before certain symbol 
Python :: python update python 
Python :: Python of if...else 
Python :: Python Using Global and Local variables in the same code 
Python :: py random.smple 
Python :: flask logging miguel grinberg 
Python :: fancy index python 
Python :: osmapi 
Python :: can we use for loop inside if statement 
Python :: encrypt 
Python :: comment analyser la différence de pixel entre deux images python 
Python :: django register form return a 302 request 
Python :: django save object 
Python :: binning continuous values in pyspark 
Python :: rickroll on input IN PYTHON 
Python :: sample regression algorithm using pipeline with hyperparameter tuning 
Python :: stacked percentage bar chart 
Python :: <h1</h1 
Python :: dht22 micropython library 
Python :: connection to python debugger failed: socket closed 
Python :: python import a filename given as string 
Python :: how to make a value 0 if its negatice 
Python :: 1046 - Game Time 
Python :: TAKING LIST INPUT IN PYTHON QUESTION 
Python :: cv2 pink color range 
Python :: how to add previous and next in tkinter in python 
Python :: addDataToExp() psychopy 
Python :: shorten all floats in a list 
Python :: how to use the dot lower function 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =