Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Challenge: Exception Logging Context Manager | Practice with Context Managers
Python Context Managers

bookChallenge: 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.

Tâche

Swipe to start coding

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.

Solution

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 2
single

single

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

How do I create a custom context manager to log exceptions?

Can you show an example of using a context manager for exception logging?

What are the benefits of using a context manager for exception handling?

close

Awesome!

Completion rate improved to 8.33

bookChallenge: Exception Logging Context Manager

Glissez pour afficher le menu

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.

Tâche

Swipe to start coding

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.

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 2
single

single

some-alt