This exercise mentions an exception, what is that?
Answer
If something is an error, it should not be handled by your program in a way that allows the user to continue using the program, as it can cause unexpected behavior and issues. They should be fixed!
An exception, on the other hand, is something that you might want to allow your program to handle. Later you’ll come across something called catching exceptions, and that’s what this means. Exceptions typically don’t cause your program to behave in unexpected ways if they occur, so long as you handle them properly.
get_user_input = raw_input("enter an integer: ")
trying_convert = int(get_user_input)
we ask the user for a integer by prompting for input with raw_input. But raw_input stores the result as string, so we need to convert to integer.
If the users enters 5, converting to integer goes fine. But if the enters hello, python will raise an error, given this can’t be converted to integer
The error raised by python will exit the program
If we then want to give the user another chance of entering input, we will first need to make an exception for the error so our program can continue running
So in other words, an exception is just something to allow someone to still run the program after an error? I.e. incorrect password try again? I understand it may not be exactly like that, but that was the closest analogy I could think of.
errors produces by python, like a ValueError (like in my earlier example) or a NameError.
lets say you have this code:
print x
you will get a NameError (x isn’t defined). This we could prevent by making exception:
try:
print x
# except keyword which allows us to create an exception
# then the exception we expect, in this case a NameError
except NameError:
print "x is not defined"
no, that would be more something like:
# not secure, don't use this anywhere important
# it just serves as an example
# give the user 3 attempts to enter there password
for i in range(3):
password = raw_input("enter your password: ")
# check length of password
if len(password) > 7:
print "valid password"
break
else:
# 3 incorrect passwords where entered.
# because i use for/else, the else clause only runs
# when loop condition becomes false
# not when the loop breaks
print "invalid password"
So password validation is not a python exception, a password validation is just code we write, and then print a message to user.
often you can drive your car with the brake lightly on. it is bad for the car, it will probably break or wear out the brakes, you won’t be able to get up to speed, but you can often make the car move even with the brake on. So that would be an exception.
if there is no fuel in the tank your car won’t be able to drive your car at all. so that would be an error.
“”"
The try block will generate an error, when the user enters a non integer value
“”"
print(‘An example of Handling errors and exceptions:’)
try:#The try block will generate an exception
get_user_input = input("enter an integer: ")
trying_convert = int(get_user_input)
print(‘Excellent, you entered an integer as required.’)
except:
print(“An exception occurred”)
Lord Noob explanation, hopefully, helps! My current understanding!
Execution is simply a type of error, commonly there are two types of errors syntax errors and exceptions.
Syntax error, the most common error erupts when a parser is unable to understand a line of code. Syntax errors are fatal, for the most part, resulting in unsuccessful code execution. Some syntax errors can be handled when detected.
Execution errors are less fatal and can be syntactically correct! The execution error occurs when python knows what to do with a piece of code, but is unable to ( Potentially something other than a syntax error)
A great example I discovered was attempting to access the internet through python, but not having an internet connection. Python interpreter can implement the command but is unable to perform because of the lack of internet connection.
Simply put, an exeption is “AN ANTICIPATED ERROR”, hence we would term it as an exception and not an error . In order for it not to be an ERROR (which would interfere with the program), it must be HANDLED, thus the term exception.
EXAMPLE:
You want to create a function to add two numbers
def add( a, b):
return a + b
print(add(1, 2)) # right use
print(add(1, "ab")) # wrong use: type error
if the user decided to input a number and string, instead of two numbers (or possibly two strings), you’d get a TypeError. You can handle a case (no more an error since you anticipate it) using a try except block like the function below which returns 7 is the user decides to play around:
def add(a, b):
try:
return a + b
except TypeError:
return 7
print(add(1, "ab")) # returns 7, error handled!