Errors and Exceptions Flashcards

1
Q

What does the interpreter provide in terms of Error Checking?

A
  • Line Number
  • Error Type
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an Exception?

A

An error that occurs during program execution and disrupts the normal flow of a program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write down a code that mitigates a ZeroDivisionError from user input taken

A
try:
    value = input()
    result = str(5 / int(value))
    print("Your result is: " + result)
except ZeroDivisionError:
    print("You tried dividing by Zero")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Write down a code that mitigates file not found error attempting to read a file

A
try:
    file_name = input()
    my_file = open(file_name, "r")
    print(my_file.read())
    my_file.close()
except FileNotFoundError:
    print("File not found")
    
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to ensure some code runs as part of the try statement regardless of whether or not an exception occurred? Give a simple example.

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to raise an exception by yourself?

A

You can do this by using the “raise” command, followed by the exception that you’d like to raise

try:
      raise ZeroDivisionError
finally:
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

If you wanted to add to variables (a + b) but instead wrote (a - b). Which error would be raised? How to fix?

A
  • “logical error”
  • Debugging
  • Process of finding and fixing logical error
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What mitigation is there for debugging? How to use it?

A
  • Logging
  • Better understanding of application flow
  • Improved bug and error tracking.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which levels of severity are in python?

A

debug
info
warning
error
critical

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to create a log entry?

A
  • 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")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is Pythons Default logging level?

A

Warning
Only messages of a level equal to or more severe than the warning level will be logged

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to customize the logging function within python?

A

logging.basicConfig()

Allows you to change:
* Level of log output
* Write logs to files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Give an example of how to change logging level

A
  • Set level parameter in the call to basicConfig
    logging.basicConfig(level=logging.DEBUG)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Give an example of writing logs to a file

A
import logging
logging.basicConfig(filename="mylog.log", filemode="w", level=logging.DEBUG)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to customize logs? Explain and give example

A

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")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which line of code will set log level to warning?

A

logging.basicConfig(level = logging.WARNING)