Exceptions in Python

Exceptions in Python

We can raise an exception very easy:

>>> f = 6
>>> if f  == 6:
…     raise RuntimeError(“Nie mogę kontynuować, bo f jest równe 6”)

Traceback (most recent call last):
  File “<stdin>”, line 2, in <module>
RuntimeError: Nie mogę kontynuować, bo f jest równe 6
>>>

We can create our own exception type. First, we define the class for our exception:

>>> class FException(RuntimeError):
…     pass

Then, we can try it:

>>> f = 6
>>> if f == 6:
…     raise FException(“Nie mogę kontynuować, bo f jest równe 6”)

Traceback (most recent call last):
  File “<stdin>”, line 2, in <module>
__main__.FException: Nie mogę kontynuować, bo f jest równe 6
>>>

It works! Let’s see other value for f:

>>> f = 80
>>> if f == 6:
…     raise FException(“Nie mogę kontynuować, bo f jest równe 6”)

>>>

There’s no exception. Yes, that’s it. However, we can extend this exception system in Python.

try:

the program code

except:

the code that will be executed when there will be an error

else:

the code will be executed when there won’t any error

We can catch and handle an exception if we know our code can do that. An easy way to show how Exceptions in Python work:

>>> try:
…     t = open(“ert.txt”, “w”)
…     t.write(“My”)
except IOError:
…     print(“Błąd!”)
else:
…     print(“Sukces!”)

2
Sukces!
>>> t.close()

And…
>>> try:
…     t = open(“ert.txt”, “r”)
…     t.readline()
except IOError:
…     print(“Błąd!”)
else:
…     print(“Sukces!”)

‘My’
Sukces!
>>> t.close()

The program is running without any errors, but the code is secured if need be. So let’s try to make an error:

>>> try:
     t = open(“eka.txt”, “r”)
…     t.readline()
except IOError:
…     print(“Błąd!”)
else:
     print(“Sukces!”)

Błąd!
>>>

For people who don’t speak in Polish, I should explain that “Błąd” is “Error” in English.

try:

the program code

finally:

the code will be executed when there won’t any error or be an error

if there’s any error, it won’t appear, and the program will go to the instruction after finally:

>>> try:
…     t = open(“et.txt”, “w”)
…     t.write(“Olo”)
… finally:
…     t.close()

3
>>>

Great, everything has been successful.

>>> try:
…     t = open(“ert.txt”, “r”)
…     u = t.readline()
… finally:
…     t.close()

And again:

>>> try:
…     t = open(“ert.txt”, “r”)
…     u = t.readline()
…     print(u)
… finally:
…     t.close()

Olo

But…
>>> try:
…     t = open(“eu.txt”, “r”)
…     u = t.readline()
… finally:
…     t.close()

Traceback (most recent call last):
  File “<stdin>”, line 2, in <module>
FileNotFoundError: [Errno 2] No such file or directory: ‘et.txt’
>>>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s