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

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 :: python how to run code in ssh 
Python :: how to create list of objects in python 
Python :: run only few test cases in pytest 
Python :: python using set 
Python :: how to remove a string in python 
Python :: service 
Python :: dfs algorithm 
Python :: args in python 
Python :: self keyword in python 
Python :: .extend python 
Python :: add new column of dataframe 
Python :: Install Python2 and Python 3 
Python :: floor function in python 
Python :: stack.pop() 
Python :: interfaces in python 
Python :: how to make loop python 
Python :: import from parent module package python 
Python :: image to vector conversion function 
Python :: django select_related and prefetch_related 
Python :: pivot tables pandas explicación 
Python :: tkinter hide legend 
Python :: Search for a symmetrical inner elements of a list python 
Python :: metodo estatico de python 
Python :: python russian roulette 
Python :: transpose([[1],[2],[3]]) 
Python :: print prime nos from 1 to n 
Python :: #finding the similarity among two sets and 1 if statement 
Python :: labelling row in python 
Python :: gridTraveler python 
Python :: django admin link column display links 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =