Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I always add an exception handler when a new method is created, and I don't know if it is a good or bad practice. I thought it may be a bit of an annoyance when viewing the source code.

Is it a good practice to put logging functions in the exception block?

def insert_new_camera(self, camera, records, file_hdlr):
    except BaseException  as e:
        raise e
share|improve this question
up vote 35 down vote accepted

Don't do that.

If you log your exceptions at every function, you'll print the same exception over and over and over again. Its only useful the first time, after that its a pain.

It will also make your code much harder to read and write.

You should log the exception when you would otherwise lose it. At some point, your exception has to be caught and not rethrown. This may happen in the default exception handler. That's where you should log. Don't log exceptions that you just rethrow. Trust whoever catches them to log them.

share|improve this answer
Also, he'll end up with a stack trace that goes no higher then one function, and he should just do raise instead of raise e. – Gabe Feb 14 '14 at 7:30

In general, with most languages, it's better not to handle exceptions until you absolutely need to. That could be just before the page crashes, you want to log the error and redirect the user to an error page.

Exceptions, by definition, are exceptional. If you can consistently expect them, you can usually code away from having to deal with them at all, and if you don't know what to do with the exception when it happens, it's best not to do anything at all, and just let it bubble up to the top of the application. As Winston said "At some point, your exception has to be caught and not rethrown".

share|improve this answer

There are times when you can't do anything with exceptions. Running out of memory is one example.

In the programming languages I've used, for private non-exposed methods/functions I don't put any, unless there is a good reason (like its critical I track this problem, or provide a more helpful error to improve the existing error message). In these cases, I blindly assume that the cleansing has already been applied to any data passed in to it.

Public methods/functions usually have some form of checking for invalid inputs, as they are the public API into your system. This would be the place to have them, as all input is evil.

Also exception handling is more to do with dealing with the system in an inconsistent state. Is it recoverable? This is where I personally draw the line. If there is a threat of internal state being corrupted, throw it.

share|improve this answer

It's not recommended nor economical to put try-catch in every function and block code. Instead, create a separate class for exception handling and log exceptions here, then show or handle these exceptions.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.