Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • How can I decide which exception to catch ? Commented Jan 19, 2014 at 4:44
  • @yopy: If you want to catch a specific exception and not others, do that: except ZeroDivisionError as e:. If you want to catch any exception except for a few special cases, except Exception as e:. If you want to catch even those special cases, except BaseException as e:. Commented Jan 19, 2014 at 4:46
  • So python wrote a class called ZeroDivisionError exception somewhere right, so what that class is doing actually, just to print the exception argument as string with the exception name ? Commented Jan 19, 2014 at 4:49
  • Yes—and in fact, you can find that class in builtins and raise it yourself. See Built-in Exceptions for a list of the classes. Meanwhile, any exception class can define its own constructor, __str__, __repr__, etc. just like any other type, but most of them just inherit the default from the base (which, as you say, just prints the exception argument(s) as a string with the exception name). Commented Jan 19, 2014 at 4:51
  • See User-defined Exceptions in the tutorial if you want to know more about how to define useful exception classes. Commented Jan 19, 2014 at 4:52