Lecture 3 - Exception Flashcards
Exceptions that require you to double-check that you typed in your code correct.
Syntax Errors
Exceptions created by unexpected behavior within your code.
Runtime Errors
Is an exception that occurs when a function receives an argument of the correct data type but an inappropriate value
ValueError
This block lets you test a block of code for errors.
Try
This block lets you handle the error.
Except
This block lets you execute code when there is no error.
Else
This block lets you execute code, regardless of the result of the try- and except blocks.
Finally
This exception is raised when the program attempts to access or use a variable that has not been defined or assigned a value.
NameError
This will let you break out of a loop, but it will also return a value.
Return
This statement is used as a placeholder for future code. When executed, nothing happens, but you avoid getting an error when empty code is not allowed.
Pass
Try & Except Syntax
try:
x = int(input(“What’s x?”))
print(f”x is {x}”)
except ValueError:
print(“x is not an integer”)
This keyword is used to break out a for loop, or a while loop.
Break
Break & Else Syntax
while True:
try:
x = int(input(“What’s x?”))
except ValueError:
print(“x is not an integer”)
else:
break
print(f”x is {x}”)
Return Syntax
def get_int():
while True:
try:
x = int(input(“What’s x?”))
except ValueError:
print(“x is not an integer”)
else:
return x
Pass Syntax
def get_int():
while True:
try:
return int(input(“What’s x?”))
except ValueError:
pass