n = 5
d = 0
try:
n / d
except ZeroDivisionError:
print("Cannot divide by zero.")
# x is not defined anywhere
try:
print(x)
except NameError:
print("Variable not defined.")
s1 = 'Word1'
s2 = 'Word2'
try:
s1/s2
except TypeError:
print("Cannot divide string.")
# [ ] Write an expression to raise a `TypeError` exception
var1="word"
var2="throne"
try:
var1/var2
except TypeError:
print("You cannot divide strings. Words don't math.")
# [ ] Write an expression to raise an `NameError` exception
try:
print(stank)
except NameError:
print("You haven't defined the stank.")
try:
5/0
except ZeroDivisionError:
print("Cannot divide by zero!")
print("Program is still running")
n = input("Please enter a number: ")
d = input("Please enter a number: ")
try:
print(n / d)
print("print executed")
except TypeError:
print("Cannot divide strings")
except ZeroDeivisionError:
print("Cannot divide by zero!")
# Without handling unexpected exception
x = 5
try:
5 + "hello"
except TypeError:
print("Cannot add an integer and string together!")
# Handling unexpected exception
x = "5"
try:
x / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
except:
print("Unexpected error")
n = input("Please enter a number: ")
d = input("Please enter a number: ")
try:
print(n / d)
print("print executed")
except TypeError:
print("Cannot divide strings")
except ZeroDeivisionError:
print("Cannot divide by zero!")
except Exception as exception_object:
print("Unexpected error: {0:}".format(exception_object))
print("Because of the try...except, this line runs even if an exception is thrown.")
# Without exception handling
from math import sqrt
x = -3
try:
sqrt(x)
# With exception handling
from math import sqrt
x = -3
try:
sqrt(x)
except ValueError as exception_object: # Storing the error message in exception_object
print(exception_object)
"""
This program tries to retrieve an integer from the user. If the user enters
something other than an integer, the program handles it by printing an error.
"""
try:
my_number = int(input("Enter an integer: "))
print("Your number: " + str(my_number))
except ValueError:
print("That wasn't an integer!")
# [ ] The following program adds first_name, last_initial and month and day of a birthdate.
# Find all exceptions that will be generated by this program,
# then handle the exceptions by displaying meaningful messages.
# You should also handle unexpected exceptions.
try:
first_name = input("Please enter your first name: ")
l_name = input("Please enter your last initial: ")
month = int(input("Please enter your the month of your birth 1-12: "))
day = int(input("Please enter the day of your birthdady 1-31: "))
print(first_name + l_name + string(month) + string(day) + "@gmail.com")
except NameError:
print("To cast as a string use str.")
except ValueError:
print("Cannot cast as int.")
except:
print("Unexpected error.")
finally:
print(first_name + l_name + str(month) + str(day) + "@gmail.com")
# [ ] The following program asks the user for their name and age.
# If the user enters an invalid value for age, the program should terminate.
# Use exception handling to deal with unexpected user input and display a meaningful message.
# Ask user for name and age.
# Enter default value for age in case they do not enter an integer
name = input("Enter your name: ")
age = -1
# Use try...except to check for a valid age
# Print name and age, using default age if user did not enter an integer
x = 3
y = 2
try:
print(x/y)
except ZeroDivisionError:
print("Cannot divide by zero")
else:
print("All good! No exceptions were raised.")
x = 3
y = 2
try:
print(x/y)
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("Code that will run whether an exception was raised or not")
x = 3
y = 0
try:
print(x/y)
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("Code that will run whether an exception was raised or not")
# [ ] The following program asks the user for an integer `x` then assigns `y` as the result of dividing `x` by 2.
# If the user enters an invalid value (i.e. "4.3" or "Hello"), the program terminates.
# Use exception handling to deal with unexpected user inputs, then use an `else` clause to calculate the value of `y`.
try:
x = input("Enter an integer: ")
y = None
x = int(x)
y = x / 2
except ValueError:
print("Input a whole number.")
except ZeroDivisionError:
print("Cannot divide by zero.")
except:
print("Unexpected exception.")
else:
print(y)
# [ ] Use the previous code and add a 'finally' clause to have the program tell the user goodbye.
try:
x = input("Enter an integer: ")
y = None
x = int(x)
y = x / 2
except ValueError:
print("Input a whole number.")
except ZeroDivisionError:
print("Cannot divide by zero.")
except:
print("Unexpected exception.")
else:
print(y)
finally:
print("Goodbye!")