0

I have a simple try and except statement. However I want to use logger exception to log the exception. What is the best way of only have 1 line of code for the logger.exception. In the exception base class ?

try:
    do_something()
except CustomBaseExecption, exc:
    logger.exception("Exception Raised:")
    raise GeneralError(exc)
except Exception as exc:
    logger.exception("Exception Raised:")
    raise GeneralError("Unknown Error")

1 Answer 1

1

Only thing that's changed between two code blocks is GeneralError argument. Let's put a conditional there.

try:
    do_something()
except Exception as exc:
    logger.exception("Exception Raised:")
    raise GeneralError(exc if isinstance(exc, CustomBaseExecption) else "Unknown Error")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.