Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära 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.

Uppgift

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ösning

solution.cpp

solution.cpp

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 2
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

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

Svep för att visa menyn

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.

Uppgift

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ösning

solution.cpp

solution.cpp

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 2
single

single

some-alt