05 Exceptions, Debugging Flashcards
What are some of the reasons to use exceptions in our code?
in care of error…
- they give users clear information on what went wrong
- they can terminate the program properly
- they can fix the error and continue the program
What does it mean, when we “raise” an exception?
It means that an exception is created. It carries information on what went wrong.
What does it mean to “catch” an exception?
It means that the programer expected an error and if that expected error actually occurs, the programmer has already written some code on how to handle it.
What happens if an exception is raised but not caught?
The program execution jumps to the end of the program and stops.
How can an exception be caught?
If we except some code to result in an error, this code needs to be put inside a “try”-block, followed by an “except”-block.
If the code inside the “try”-block leads to the specified error, the execution instantly jumps to the “except”-block and continues executing there.
In regards to exceptions, what does the keyword ‘finally’ do?
The optional keyword is put at the end of a try-except-structure. In this finally-block we put code that needs to be executed in any case, no matter how the exception is handled in the except-block.
What are some of the predefined exception types we’ve learned?
- TypeError (incompatible data type)
- ValueError (correct type but incorrect value)
- IndexError (index out of range)
- KeyError (key not found in dictionary)
- ZeroDivisionError
- FileNotFoundError
Assume we have a function func1() that calls func2() and
this function again calls func3().
Some error occurs in func3().
Explain the order in which the handling of the error is looked for.
Reverse order:
Check if the exception is caught in func3(). If not, jump to the code where func3() was called in func2() and check if the exception is caught there.
Continue upwards.
If the exception isn’t caught anywhere, the program ends with an error.
What kind of information can we find in an exception output?
- name of exception
- detailed error message
- traceback (where did the exception occur?)
What can be done if some code could possibly raise different kinds of error?
Multiple exceptions can be caught by putting more than one except-block after the try-block.
True or False: Code can throw multiple exceptions at once, for example a TypeError and ValueError, if both the data type and the value are incorrect.
False. Only one except-block is ever executed. The execution is done from top to bottom, the first matching except-block is executed.
What is the difference between the following two exception-structures?
- try:
func()
except ValueError:
print(“oops”)
finally:
print(“damn”) - try:
func()
except ValueError:
print(“oops”)
else:
print(“damn”)
- if the function leads to a ValueError, “oops” is printed, then “damn” is printed. If it doesn’t lead to an error, nothing is printed.
- if the function leads to a ValueError, “oops” is printed. If it doesn’t lead to an error, “damn” is printed.
In debugging, what are breakpoints?
When running the code in debug-modus, it will pause the execution at the previously specified points in the code. This allows you to check the current values of variables, slowly step through the following lines of code or dive into functions.
In Debugging, what is the call stack?
It’s a panel that shows the sequence of function calls that lead to the current point in your code.
What kinds of special breakpoints are there?
- conditional breakpoints: only stops the execution at this line of code if the condition is true.
- logging breakpoints: they write messages to the console