Errors and files

Handle problems without crashing, and read and write files safely.

Bad input should not crash your program. Wrap the risky line in try, and name the exact error you expect.

Expected output
42
0

finally

Expected output
Cannot divide by zero
Done

Files

Use with, which closes the file for you even if something goes wrong. "w" writes, "a" adds to the end, and "r" reads.

Expected output
Line one
Line two
โ† FunctionsClasses and objects โ†’