Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Logging and Rethrowing | Advanced Exception Handling
C++ Exception Handling

bookChallenge: Logging and Rethrowing

In many real-world applications, you may need to log information about exceptions before allowing them to propagate further up the call stack. This helps with debugging and monitoring, while still allowing higher-level code to make decisions about how to handle the error. Your challenge is to write a function that catches an exception, logs a specific message, and then rethrows the same exception so that it can be handled elsewhere.

Aufgabe

Swipe to start coding

Write a function named logAndRethrow that takes a callable object (such as a lambda or function) as its parameter. The function should execute the callable inside a try block. If any exception is thrown, your function should catch it, print the message Exception caught and logged, and then rethrow the same exception. Do not modify or suppress the original exception; ensure it can still be caught by code outside of logAndRethrow.

Lösung

solution.cpp

solution.cpp

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 2
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you show me an example of how to implement this function in Python?

What kind of logging should I use—print statements or a logging library?

Can you explain why rethrowing the exception is important in this context?

close

Awesome!

Completion rate improved to 6.67

bookChallenge: Logging and Rethrowing

Swipe um das Menü anzuzeigen

In many real-world applications, you may need to log information about exceptions before allowing them to propagate further up the call stack. This helps with debugging and monitoring, while still allowing higher-level code to make decisions about how to handle the error. Your challenge is to write a function that catches an exception, logs a specific message, and then rethrows the same exception so that it can be handled elsewhere.

Aufgabe

Swipe to start coding

Write a function named logAndRethrow that takes a callable object (such as a lambda or function) as its parameter. The function should execute the callable inside a try block. If any exception is thrown, your function should catch it, print the message Exception caught and logged, and then rethrow the same exception. Do not modify or suppress the original exception; ensure it can still be caught by code outside of logAndRethrow.

Lösung

solution.cpp

solution.cpp

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 2
single

single

some-alt