0

When you use try/except in python, why do you need an exception type to be named after except? Wouldn't it be easier to just catch all exceptions?

try:
    #dosomething
except Exception:
    #dosomething

Why is the 'Exception' needed after except?

4 Answers4

4

From a purely syntactic point of view, this is acceptable code:

try:
    # Do something
except:
    print "Something went wrong."

HOWEVER, it's not a very good idea to do this a lot of the time. By catching all exceptions and not even saving the name, you're losing all information about where the error was. Just seeing Something went wrong. printed out is both useless and frustrating. So even if you don't want to handle each exception individually, you'd want to save the exception information at the very least.

try:
    # Do something.
except Exception, e:
    print "Encountered error " + str(e) + " during execution. Exiting gracefully."

The above code is something you might do if you absolutely can't let your program exit abruptly, for example.

EDIT: changed the answer to clarify that it's a bad idea, though possible.

TheSoundDefense
  • 6,753
  • 1
  • 30
  • 42
4

Because you might handle different exceptions different ways.

For example, if you're attempting a network operation, and the network address you're trying to reach can't be resolved, that's likely due to user error, and you'll want to get the user involved, while some other kinds of errors can simply be retried after a short wait.

It's a good practice in exception handling to handle only the narrowest set of exceptions you expect at any one point in the code, and to only catch those exceptions that you're sure you know how to handle. A catch-all exception handler violates this principle.

Russell Borogove
  • 18,516
  • 4
  • 43
  • 50
1

Why and how to catch exceptions

Exceptions are really helpful, but they shall be handled in different manner depending on what code you write.

Core distinction is, if your code is top level one (last resort to handle exceptions) or inner one.

Another aspect is, if some exceptions are excepted or unexpected.

Expected exceptions (like file, you are trying to use is missing) shall be handled, if the code has a chance to do anything about it.

Unexpected exceptions shall not be handled unless you have to do so in top level code.

Handling exceptions in top level code

If it does not matter, that your code throws a bit ugly stack trace under some circumstances, simply ingore the unexpected exceptions. This is mostly very efficient, as the code is kept simple, and stack trace give you good chance to find out, what went wrong.

If you have to make your script "well behaving" - you could catch the exception and print some nice looking excuse for what went wrong.

Handling exceptions in lower level code (modules, functions)

In your lower level code, you shall catch all expected exceptions, and the rest shall be ignored and thrown up to higher levels, where is better chance to handle it properly.

If you have no expected exception, simply do not use try .. except block.

Printing some excuses from lower level code is mostly inappropriate (higher level code has no chance t silence your printouts.

To your question - why except Exception

except with explicitly mentioned type of exception is the only solution to use for expected types of exceptions. Without mentioning the type (or types), you are catching all and this is bad habit unless you are in top level code.

As usual, there are exceptions to the recommendations above, but they are occurring less often than one usually expects.

Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98
0

Different exceptions require different fixing. For example, when I was writing a python irc bot, i would have one exception for invalid access to a string, and that code in the except would try to remedy it. I also had one for bad sockets that would try to deduce why it went bad and fix it. I can't have these under one exception because they are fixed differently

DTSCode
  • 1,062
  • 9
  • 24