You may see this chunk of red text when an error occurs
This chunk of red text when an error occurs is helpful as it tells us the exact line of the error and its type
Using the image below, what line has the error occurred in?
Line 10
Using the image below, what type of error has occurred?
NameError
Exception Handling is where a complete program should have a code in place for when an unexpected error occurs
In this example, Python will attempt to run the code indented beneath "try:" - If there are no errors , the code will stop just before "except" - If an error does occur, the exception code will be run
In the code below, if we enter a correct value , the program will execute normally
In the code below, if an error occurs, the exception code will run - e.g. if you enter the string "cheese" , than the code will run and say "You did not enter a number"
You can add the else command to your code - It will only execute if there are no errors
If a valid number is entered into this code, the else code will be printed (e.g. if time == 4, "How many days will you be with us" "Oh 4 days? I'll fetch your bags!")
If a code generating an error is entered , the except code will be printed (e.g. if time = umbrella, "How many days will you be with us" "Sorry, I don't understand? GET OUT MY HOTEL!")
The code below asks the user to their age in years as a number - If a valid number is generated , the else code is printed - If a code generating an error is used, the exception code will be printed
The exception command can be used for any general error that occurs
Specific exception commands can be used for a variety of errors
In the program below, how many specific exception commands are there?
2
In the code below, if the wrong data type is entered, a Value Error occurs , and this prints the relevant response
In the code below, if the user tries to divide by 0, a Zero Division Error occurs, and this prints the relevant response
In the code below, what response will be printed if a ValueError is triggered ?
You haven't entered a valid number!
In the code below, what response will be printed if a ZeroDivisionError is triggered?
You can't divide by 0 ! What were you thinking?
The code below asks the user to input a number that they can divide by 999 , and this value will be displayed - However, if an invalid value is entered, a ValueError occurs - And if you input 0, a ZeroDivisionError occurs