10. Errors and Exception Handling Flashcards

1
Q

What cause an SyntaxError?

A

print(‘Hello) # missing final ‘

File “”, line 1
print(‘Hello)
^
SyntaxError: EOL while scanning string literal

Note how we get a SyntaxError, with the further description that it was an EOL (End of Line Error) while scanning the string literal. This is specific enough for us to see that we forgot a single quote at the end of the line. Understanding these various error types will help you debug your code much faster.

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

The basic keywords for dealing with exceptions are?

A

try and except

The basic terminology and syntax used to handle errors in Python are the try and except statements. The code which can cause an exception to occur is put in the try block and the handling of the exception is then implemented in the except block of code. The syntax follows:

try:
You do your operations here…

except ExceptionI:
If there is ExceptionI, then execute this block.
except ExceptionII:
If there is ExceptionII, then execute this block.

else:
If there is no exception then execute this block.

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

How can you run a block of code regardless if there is an exception or not?

A

use the ‘finally’ keyword.

The finally: block of code will always be run regardless if there was an exception in the try code block. The syntax is:

try:
Code block here

Due to any exception, this code may be skipped!
finally:
This code block would always be executed.

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

Use ‘continue’ to loop in a while block:

A
def askint():
    while True:
        try:
            val = int(input("Please enter an integer: "))
        except:
            print("Looks like you did not enter an integer!")
            continue
        else:
            print("Yep that's an integer!")
            break
        finally:
            print("Finally, I executed!")
        print(val)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does pylint do?

A

pylint tests for style as well as some very basic program logic.

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

What does unittest do?

A

unittest lets you write your own test programs. The goal is to send a specific set of data to your program, and analyze the returned results against an expected result.

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

What does an unittest look like?

A

%%writefile test_cap.py
import unittest
import cap

class TestCap(unittest.TestCase):

    def test_one_word(self):
        text = 'python'
        result = cap.cap_text(text)
        self.assertEqual(result, 'Python')
    def test_multiple_words(self):
        text = 'monty python'
        result = cap.cap_text(text)
        self.assertEqual(result, 'Monty Python')

if __name__ == ‘__main__’:
unittest.main()

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