Error and Exception Handling · 17 / 20 Exercise Medium

Chained errors

Objective

When you catch a low-level error and raise your own, you should chain them with raise ... from e: the new error explains what failed and __cause__ keeps the original cause.

Instructions

  • The first line has the number n of lines; each one is name value unit (the value is an integer and the unit is kb or mb).
  • Define ConfigError. If the line is valid, show name = bytes with the value in bytes (1 KB = 1024 bytes, 1 MB = 1024² bytes). If not (wrong format, non-numeric value or unknown unit), raise ConfigError with message invalid line chaining the original error, and show Line k: invalid line (cause: ErrorType).
Input
3
cache 10 kb
disco x mb
logs 5 gb
Output
cache = 10240
Line 2: invalid line (cause: ValueError)
Line 3: invalid line (cause: KeyError)

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise