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

python catch any exception

try:
    do_something()
except:
    print "Caught it!"
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

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 :: drop null values in dataframe 
Python :: how to add number to string in python 
Python :: how to iterate tuple in python 
Python :: pyqt5 buttons 
Python :: thresholding with OpenCV 
Python :: python add 1 
Python :: do i need do some set when i use GPU to train tensorflow model 
Python :: how to add one to the index of a list 
Python :: python code for twitter scraping using tweepy 
Python :: python boto3 put_object to s3 
Python :: Odd number without loop in python 
Python :: how to get spotify playlist id in spotipy 
Python :: Generation of Random Numbers in python 
Python :: prettify json in pycharm 
Python :: python encoding declaration 
Python :: geometric progression in python 
Python :: python tkinter get entry text 
Python :: how to access dictionary inside an array python 
Python :: string to list of characters python 
Python :: python sched 
Python :: AttributeError: __enter__ in python cde 
Python :: python program to find sqaure root of the number 
Python :: como agregar elementos a un array en python 
Python :: python while loop and recursion 
Python :: change group box title font size 
Python :: selenium text value is empty in flask returns 
Shell :: error: cannot install "code": classic confinement requires snaps under /snap or 
Shell :: git store credential 
Shell :: filename too long git 
Shell :: git set email and username 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =