diff --git a/docs/source/book/06_control_structures/6_exceptions.rst b/docs/source/book/06_control_structures/6_exceptions.rst index a115c59..a964259 100644 --- a/docs/source/book/06_control_structures/6_exceptions.rst +++ b/docs/source/book/06_control_structures/6_exceptions.rst @@ -8,7 +8,7 @@ If you repeated examples that were used before, there could be situations where Python generally reacts quite understandably to such errors and they can easily be corrected. -However, even if the code is written correctly, errors can occur. In Python, these errors are called **exceptions**. +However, even if code is written correctly, errors can occur. In Python, these errors are called *exceptions*. Examples of exceptions: @@ -22,18 +22,18 @@ Examples of exceptions: ----------------------------------------------------- TypeError: must be str, not int -There are two exceptions: **ZeroDivisionError** and **TypeError**. +There are two exceptions: *ZeroDivisionError* and *TypeError*. -Most often, it is possible to predict what kind of exceptions will occur during the execution of the program. +Most often, it is possible to predict what kind of exceptions will occur during execution of the program. -For example, if the program expects two numbers on the input and at the output returns their sum, and the user has entered a string instead of one of the numbers a TypeError error will appear as in the example above. +For example, if program expects two numbers in input and output returns their sum and user has entered a string instead of one of numbers a TypeError error will appear as in example above. Python allows working with exceptions. They can be intercepted and acted upon if an exception has been occurred. .. note:: - When an exception appears, the program is immediately interrupted. + When an exception appears, program is immediately interrupted. -In order to work with exceptions the ``try/except`` construction is used: +In order to work with exceptions ``try/except`` construction is used: .. code:: python @@ -46,13 +46,13 @@ In order to work with exceptions the ``try/except`` construction is used: The **try** construction works as follows: -* first execute the expressions that are written in the **try** block -* if there are no exceptions during the execution of the **try** block, the block **except** is skipped and the following code is executed -* if there is an exception within the **try** block, the rest part of the **try** block is skipped - * if **except** block contains an exception which has been occurred, the code in **except** block is executed - * if the exception that has raised is not specified in **except** block, the program execution is interrupted and an error is generated +* first execute expressions that are written in **try** block +* if there are no exceptions during execution of **try** block, block **except** is skipped and the following code is executed +* if there is an exception within **try** block, the rest part of **try** block is skipped + * if **except** block contains an exception which has been occurred, code in **except** block is executed + * if exception that has raised is not specified in **except** block, program execution is interrupted and an error is generated -Note that the ``Cool!`` string in the **try** block is not displayed: +Note that ``Cool!`` string in **try** block is not displayed: .. code:: python @@ -66,9 +66,9 @@ Note that the ``Cool!`` string in the **try** block is not displayed: Let's divide some numbers You can't divide by zero -Construction try/except may have many **except** if different actions are needed depending on the type of error. +Construction try/except may have many **except** if different actions are needed depending on type of error. -For example, the divide.py script divides two numbers entered by the user: +For example, divide.py script divides two numbers entered by user: .. code:: python @@ -102,9 +102,9 @@ Examples of script execution: Enter second number: 3 Please enter only numbers -In this case, the ValueError exception occurs when the user has entered a string instead of a number. +In this case, ValueError exception occurs when user has entered a string instead of a number. -The ZeroDivisionError exception occurs if the second number is 0. +ZeroDivisionError exception occurs if second number is 0. If you do not need to display different messages on ValueError and ZeroDivisionError, you can do this (divide\_ver2.py file): @@ -144,7 +144,7 @@ try/except/else Try/except has an optional **else** block. It is implemented if there is no exception. -For example, if you need to perform any further operations with the data that the user entered, you can write them in the **else** block (divide_ver3.py file): +For example, if you need to perform any further operations with data that user entered, you can write them in **else** block (divide_ver3.py file): .. code:: python @@ -176,7 +176,7 @@ Example of execution: try/except/finally ~~~~~~~~~~~~~~~~~~ -The **finally** block is another optional block in **try** construction. It is *always* implemented, whether an exception has been raised or not. +Block **finally** is another optional block in **try** construction. It is *always* implemented, whether an exception has been raised or not. It’s about actions that you have to do anyway. For example, it could be a file closing. @@ -222,9 +222,9 @@ Verification: When to use exceptions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -As a rule, the same code can be written with or without exceptions. +As a rule, same code can be written with or without exceptions. -For example, this version of the code: +For example, this version of code: .. code:: python @@ -259,6 +259,6 @@ You can rewrite this without try/except (try_except_divide.py file): But the same option without exceptions will not always be simple and understandable. -It is important to assess in each specific situation which version of the code is more comprehensible, compact and universal - with or without exceptions. +It is important to assess in each specific situation which version of code is more comprehensible, compact and universal - with or without exceptions. -If you’ve used some other programming language before, it’s possible that the use of exceptions was considered as a bad form. In Python this is not true. To get a little bit more into this issue, look at the links to additional material at the end of this section. +If you’ve used some other programming language before, it’s possible that use of exceptions was considered as a bad form. In Python this is not true. To get a little bit more into this issue, look at the links to additional material at the end of this section.