Skip to content

Latest commit

 

History

History
executable file
·
51 lines (33 loc) · 991 Bytes

File metadata and controls

executable file
·
51 lines (33 loc) · 991 Bytes

try-except

Examples in Python 3

"""
Try-except example with string to integer conversion
"""
# The string is numeric - int() will succeed
x = "42"
print(x, "is of type", type(x))

try:
	x = int(x)
	print(x, "is of type", type(x))
except:
	print("Could not convert", x, "to integer")

print(x, "is the answer to everything\n")


# The string is not numeric - int() will fail and throw an exception
x = "fortytwo"
print(x, "is of type", type(x))

try:
	x = int(x)
	print(x, "is of type", type(x))
except:
	print("Could not convert", x, "to integer")

print(x, "is the answer to everything")
	

Reference and read more

Errors and Exceptions

The try statement

Revision history

2014-07-17 (sylvanas) PA1 First try.