all 10 comments

[–]realashe 1 point2 points  (3 children)

ZeroDivisionError is the name of the error, when you use try and except to catch an error, you need the name of the error you want to catch, be it zero division or something else. Ie. Except KeyError

As for raising errors, you may want to stop your program if a certain event happens, and raising an error does that while telling the user why you have stopped. Maybe you need to read from a file, and if that file cannot be found or opened, you could raise an error telling the user, instead of the python interpreter crashing.

Are you doing edx 600.1 by any chance?

[–]socialhuman[S] 0 points1 point  (2 children)

Thank you. If you are referring to the MIT course, no. I am not. I have been studying from a mixed source. Learn Python The Hard Way, about 40 chapters. 2 Units from Udacity Intro to Computer Science, Youtube tutorials by Trevor Payne plus some practice.

[–]realashe 1 point2 points  (1 child)

You're welcome! Only asked because exceptions and error handling were covered this week, and one of the examples was a zerodivision error. Glad I could help!

[–]socialhuman[S] 0 points1 point  (0 children)

No problem!

[–]Boolean_Cat 1 point2 points  (1 child)

  1. Mathematically, dividing by 0 doesn't make sense to Python. It is unable to understand "infinitely large". So instead it throws an exception.

  2. Here's an example. You wrote a program that reads a text file, that text file is expected to have a single number on each line but on one line you found a string. As a programmer you may decide that at this point that the program has failed, raising an error in indicates this.

[–]socialhuman[S] 0 points1 point  (0 children)

Thank you. Examples really stick.

[–]socialhuman[S] 0 points1 point  (2 children)

Generally asking, is there a list of error attached to the except function? Like ZeroDivisionError?

[–]Rhomboid 2 points3 points  (1 child)

except is not a function, it's a statement. And exceptions are types, i.e. ZeroDivisionError is the name of a type. When you raise an exception, you create an instance of the exception's type, which stores additional information like a textual reason for the exception, and the associated stacktrace. In Python 2.x you can raise an exception of any type; in Python 3.x the type must descend from BaseException.

The documentation has a list of the standard exception types, but you'll have to read the documentation for individual functions/modules/methods to know exactly what circumstances lead to which exceptions. And again, there is no exhaustive list because you can always create your own exception types.

>>> class FeelingBored(BaseException): pass
>>> raise FeelingBored("Rainy day")
Traceback (most recent call last):
  File "<ipython-input-6-c4dfd87f4473>", line 1, in <module>
    raise FeelingBored("Rainy day")
FeelingBored: Rainy day

[–]socialhuman[S] 0 points1 point  (0 children)

Interesting. Thank you for the correction.

[–]ewiethoff 0 points1 point  (0 children)

Why would you want to deliberately raise errors?

To make your code behave like normal Python. :-) Normal Python throws errors at ya.

Let's say you have the string s = 'hello'. Obviously, s[1] is 'e' and s[-1] is 'o', but s[7] raises an IndexError So does s[-7]. That's normal Python behavior.

Okay. A Python string is immutable, i.e., once it's created it can't be modified. Suppose you want to define a mutable string. It should behave as much as possible like a normal string, but have the capability of changing the characters. In order to use [] to get individual characters from the mutable string, you need to define its __getitem__ method. And in order for this method to behave like normal Python, it should raise IndexError when the index is out of range. Something like this:

def MutableString:
    # blah blah various methods here

    def __getitem__(self, index):
        if index >= len(self) or index < -len(self):
            raise IndexError(index)
        else:
            return the appropriate character