01. Perform Troubleshooting and Error Handling 1 Flashcards
True or False: A Syntax error in python is similar to a grammatical or spelling error in writing.
True
Which lines of code in this example have syntax errors?
- Line 2 - The boolean false should be capitalised
- Line 5 - code is not indented
- Line 10 - elif statement is missing a colon
- Line 12 - print is spelt incorrectly
The following code contains a logic error. What is the issue?
The script is looking for “Riga” as the correct answer.
The user could enter “riga” and the script would note that as an incorrect answer.
Explain what is meant by the term “runtime error”.
These type of errors only occur when the program is run.
Name two common types of runtime errors.
- Trying to read a file that does not exist.
- Trying to divide a number by zero.
Referring to the code sample below, why would you add an else statement to a try/except block?
You would add an else statement after directly after the except block to print something if the try block did not raise an exception. Essentially this will mean that the code worked as expected.
Why would you add “finally” to a try/except block?
You may wish to display a final message to a user regardless of whether the try block was successful or not.
Referring to the code example below, what will happen if the user tries to divide a number by zero?
The program will raise the error, however, the program will still end without fully completing all of the print statements.
What keyword do we use to display an actual error message to a user?
The raise keyword.
What often causes syntax errors? Choose two answers.
Select one or more:
a. A missing colon beginning a block
b. Incorrect numbers
c. Using something where is does not fit according to the rules of writing Python
d. Coding from memory
a. A missing colon beginning a block
c. Using something where is does not fit according to the rules of writing Python
In an IDE, every syntax error is displayed with a red squiggly line beneath the flawed code.
True or false?
Select one:
True
False
False
In exception handling, the else keyword’s block of code can be used in Python to display a message of __________ to users.
a: warning
b: error unexpected
c: failure
d: success
d: success
Runtime errors occur only when an app runs, if not caught they _________ the app.
a: fix
b: crash
c: improve
d: give a warning message
b: crash
Which term describes the action of managing errors in a way that an app will not crash on a user, and if it does need to stop, a user is informed?
Select one:
a. Exception dealing
b. Exception handling
c. User benefits
d. Error management
b. Exception handling
A programmer would like to display a message to users regardless of whether the try block was successful or not. In this case, why should a finally keyword be used instead of an else keyword after the try/except block?
Select one:
a. The finally keyword is the last step in successful code
b. An else statement will not run if the try block fails
c. The else keyword can be used alone in this instance
d. A finally statement is more efficient than an else statement
b. An else statement will not run if the try block fails