single
Challenge: Exception Logging Context Manager
メニューを表示するにはスワイプしてください
In real-world Python programs, you may want to track when exceptions occur within a specific part of your code. Context managers make this process simple and consistent. By creating a custom context manager that logs exceptions, you can ensure that important errors are always noticed, even if you want the exception to continue propagating after being logged. This challenge will give you hands-on practice with the context management protocol and exception handling.
スワイプしてコーディングを開始
Create a context manager named ExceptionLogger that logs a message whenever an exception occurs inside its block. The message should include the exception type and message. After logging, the exception should still be raised as usual.
- If no exception occurs, the context manager should do nothing.
- If an exception occurs, print a message in the format:
Exception caught: <exception type>: <exception message>. - After printing, allow the exception to propagate.
解答
フィードバックありがとうございます!
single
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください