Wrap and Chain the Real Cause

Hard ⏱ 18 min 32% acceptance ★★★★☆ 4.2
Write load_port(config) that reads config['port'] and converts it to int, but translates ANY failure (missing key or bad value) into a single RuntimeError('invalid config') chained to the original using raise ... from err — so tracebacks show both the friendly error and the root cause.

Examples

Example 1
Input
load_port({'port': 'abc'})
Output
RuntimeError: invalid config (chained from ValueError)
Explanation

The from clause preserves the original as __cause__.

Constraints

  • Catch (KeyError, ValueError) in one clause.
  • Use raise ... from err.

Topics

Exception Handlingraise from

Companies

GoogleBloomberg

Hints

Hint 1

except (A, B) as err: groups the types.

Hint 2

The from keyword sets __cause__ on the new exception.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.