Errors and Exceptions Flashcards
What does the interpreter provide in terms of Error Checking?
- Line Number
- Error Type
What is an Exception?
An error that occurs during program execution and disrupts the normal flow of a program
Write down a code that mitigates a ZeroDivisionError from user input taken
try: value = input() result = str(5 / int(value)) print("Your result is: " + result) except ZeroDivisionError: print("You tried dividing by Zero")
Write down a code that mitigates file not found error attempting to read a file
try: file_name = input() my_file = open(file_name, "r") print(my_file.read()) my_file.close() except FileNotFoundError: print("File not found")
How to ensure some code runs as part of the try statement regardless of whether or not an exception occurred? Give a simple example.
- You can do this by adding a finally clause to the end of the try/except block
- Code in the finally block will always run after a try/except block regardless of errors
try: try: value = input() result = str(5 / int(value)) print("Your result is: " + result) except ZeroDivisionError: print("You tried dividing by Zero") finally: print("Here is our final block") print("End of program")
How to raise an exception by yourself?
You can do this by using the “raise” command, followed by the exception that you’d like to raise
try: raise ZeroDivisionError finally:
If you wanted to add to variables (a + b) but instead wrote (a - b). Which error would be raised? How to fix?
- “logical error”
- Debugging
- Process of finding and fixing logical error
What mitigation is there for debugging? How to use it?
- Logging
- Better understanding of application flow
- Improved bug and error tracking.
Which levels of severity are in python?
debug
info
warning
error
critical
How to create a log entry?
- Logging object, followed by period and level of log entry you wish to create (debug, info, warning, error, critical)
- Followed by log message in parentheses
logging.debug("Debug is the lowest Level in Severity") logging.critical("Critical is the fifth and highest level")
What is Pythons Default logging level?
Warning
Only messages of a level equal to or more severe than the warning level will be logged
How to customize the logging function within python?
logging.basicConfig()
Allows you to change:
* Level of log output
* Write logs to files
Give an example of how to change logging level
- Set level parameter in the call to basicConfig
logging.basicConfig(level=logging.DEBUG)
Give an example of writing logs to a file
import logging logging.basicConfig(filename="mylog.log", filemode="w", level=logging.DEBUG)
How to customize logs? Explain and give example
You can use various attributes to get more detailed info on what is going on in your program. You set these attributes in the basicConfig function by assigning a string to the format parameter
-
format
is keyword -
&
denotes data type -
asctime
is attribute outputting date and time of log creation -
message
attribute is placeholder for actual log message
logging.basicConfig(format=%(asctime)s: %(message)s") logging.critical("My log message")