Skip to content

Commit 0c7dc41

Browse files
committed
Exceptions: PEP8, typos, and more
* Make sure to follow PEP8 everywhere * Be consistent about the type of string literal quotes (I suggest to write a script or otherwise make sure that you always use the same) * Do [not use bare excepts](https://docs.python.org/2/howto/doanddont.html#except)
1 parent 7954a08 commit 0c7dc41

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

exceptions.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Here is a simple example:
1313
.. code:: python
1414
1515
try:
16-
file = open('test.txt','rb')
16+
file = open('test.txt', 'rb')
1717
except IOError as e:
1818
print('An IOError occured. {}'.format(e.args[-1]))
1919
@@ -31,7 +31,7 @@ tuple. Like so:
3131
3232
try:
3333
file = open('test.txt', 'rb')
34-
except (IOError,EOFError) as e:
34+
except (IOError, EOFError) as e:
3535
print("An error occured. {}".format(e.args[-1]))
3636
3737
Another method is to handle individual exception in a separate except
@@ -50,20 +50,20 @@ block. We can have as many except blocks as we want. Here is an example:
5050
5151
This way if the exception is not handled by the first except block then
5252
it is passed on to the second block. Now the last method involves
53-
traping ALL exceptions:
53+
trapping ALL exceptions:
5454

5555
.. code:: python
5656
5757
try:
5858
file = open('test.txt', 'rb')
59-
except:
60-
# Some loggin if you want
59+
except Exception:
60+
# Some logging if you want
6161
raise
6262
6363
This can be helpful when you have no idea about the exception which can
6464
be thrown by your program.
6565

66-
``Finally`` caluse
66+
``Finally`` clause
6767
~~~~~~~~~~~~~~~~~~
6868

6969
We wrap our main code in the try clause. After that we wrap some code in
@@ -76,7 +76,7 @@ for cleaning up after a script. Here is a simple example:
7676
.. code:: python
7777
7878
try:
79-
file = open('test.txt','rb')
79+
file = open('test.txt', 'rb')
8080
except IOError as e:
8181
print('An IOError occured. {}'.format(e.args[-1]))
8282
finally:
@@ -88,7 +88,7 @@ for cleaning up after a script. Here is a simple example:
8888
``try/else`` clause
8989
~~~~~~~~~~~~~~~~~~~
9090

91-
Often times we might want some code to run IF no exception occurs. This
91+
Often times we might want some code to run if **no** exception occurs. This
9292
can easily be achieved by using an ``else`` clause. Most people don't
9393
use it and honestly I have myself not used it widely. Here is an
9494
example:
@@ -97,12 +97,12 @@ example:
9797
9898
try:
9999
print('I am sure no exception is going to occur!')
100-
except:
101-
print("exception")
100+
except Exception:
101+
print('exception')
102102
else:
103103
print('This would only run if no exception occurs.')
104104
finally:
105-
print("This would be printed in every case.")
105+
print('This would be printed in every case.')
106106
107107
# Output: I am sure no exception is going to occur!
108108
# This would only run if no exception occurs.

0 commit comments

Comments
 (0)