Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1except sqlite3.Error: | ACatches any failed database call so the program can show a friendly message instead of crashing | |
| 2except sqlite3.IntegrityError: | BA bare except catches every kind of error, including unrelated bugs, and hides the real cause. It should be except sqlite3.Error: so only database errors are handled. | |
| 3Put the SQL call inside a try block so a constraint failure jumps to the except block instead of crashing the program | CThe handler never calls conn.rollback(), so a failed change is not undone. It should call conn.rollback() before or after printing the message. | |
| 4After a failed change, undo the half-finished work with rollback() so the database is left in a safe state | Dtry: cur.execute(sql, data) except sqlite3.Error: print("Could not save") | |
| 5try: cur.execute(sql, data) except: print("Could not save") | ECatches an error caused by breaking a constraint, such as a duplicate primary key or a NOT NULL field left empty | |
| 6try: cur.execute(sql, data) conn.commit() except sqlite3.Error: print("Failed") | Fexcept sqlite3.Error: conn.rollback() print("Change undone") |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
except sqlite3.Error: — Catches a failed database call, such as an IntegrityError from a broken constraint, so the program shows a friendly message instead of crashing. Call rollback() to undo.try:
cur.execute(sql, data)
except sqlite3.Error:try:
conn.commit()
except sqlite3.Error:except sqlite3.IntegrityError:
print("That student ID is already used")except sqlite3.Error:
conn.rollback()
print("Could not save. Try again.")try:
cur.execute(sql, data)
except sqlite3.Error: