# Errors & Exceptions

We can make certain mistakes while writing a program that lead to errors when we try to run it. A Python program terminates as soon as it encounters an unhandled error. These errors can be broadly classified into two classes:

1. Syntax errors
2. Logical errors (Exceptions)

### Syntax Errors

Error caused by not following the proper structure (syntax) of the language is called syntax error or parsing error.

Let's look at one example:

```python
>>> if a < 3
File "<interactive input>", line 1
 if a < 3
        ^
  SyntaxError: invalid syntax  
```

As shown in the example, an arrow indicates where the parser ran into the syntax error.

We can see here that a colon `:` is missing in the `if` statement.

### Logical Errors (Exceptions)

Errors that occur at runtime (after passing the syntax test) are called exceptions or logical errors.

For instance, they occur when we try to open a file(for reading) that does not exist (`FileNotFoundError`), try to divide a number by zero (`ZeroDivisionError`), or try to import a module that does not exist (`ImportError`).&#x20;

Whenever these types of runtime errors occur, Python creates an exception object. If not handled properly, it prints a `traceback` to that error along with some details about why that error occurred.

Let's look at how Python treats these errors:

```python
>>> 1 / 0
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
ZeroDivisionError: division by zero

>>> open("imaginary.txt")
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'imaginary.txt'
```

### Built-in Exceptions

Erroneous operations can raise exceptions. There are plenty of built-in exceptions in Python that are raised when corresponding errors occur.&#x20;

The table below shows built-in exceptions that are usually raised in Python:

| Exception             | Description                                                                       |
| --------------------- | --------------------------------------------------------------------------------- |
| ArithmeticError       | Raised when an error occurs in numeric calculations                               |
| AssertionError        | Raised when an assert statement fails                                             |
| AttributeError        | Raised when attribute reference or assignment fails                               |
| Exception             | Base class for all exceptions                                                     |
| EOFError              | Raised when the input() method hits an "end of file" condition (EOF)              |
| FloatingPointError    | Raised when a floating point calculation fails                                    |
| GeneratorExit         | Raised when a generator is closed (with the close() method)                       |
| ImportError           | Raised when an imported module does not exist                                     |
| IndentationError      | Raised when indendation is not correct                                            |
| IndexError            | Raised when an index of a sequence does not exist                                 |
| KeyError              | Raised when a key does not exist in a dictionary                                  |
| KeyboardInterrupt     | Raised when the user presses Ctrl+c, Ctrl+z or Delete                             |
| LookupError           | Raised when errors raised cant be found                                           |
| MemoryError           | Raised when a program runs out of memory                                          |
| NameError             | Raised when a variable does not exist                                             |
| NotImplementedError   | Raised when an abstract method requires an inherited class to override the method |
| OSError               | Raised when a system related operation causes an error                            |
| OverflowError         | Raised when the result of a numeric calculation is too large                      |
| ReferenceError        | Raised when a weak reference object does not exist                                |
| RuntimeError          | Raised when an error occurs that do not belong to any specific expections         |
| StopIteration         | Raised when the next() method of an iterator has no further values                |
| SyntaxError           | Raised when a syntax error occurs                                                 |
| TabError              | Raised when indentation consists of tabs or spaces                                |
| SystemError           | Raised when a system error occurs                                                 |
| SystemExit            | Raised when the sys.exit() function is called                                     |
| TypeError             | Raised when two different types are combined                                      |
| UnboundLocalError     | Raised when a local variable is referenced before assignment                      |
| UnicodeError          | Raised when a unicode problem occurs                                              |
| UnicodeEncodeError    | Raised when a unicode encoding problem occurs                                     |
| UnicodeDecodeError    | Raised when a unicode decoding problem occurs                                     |
| UnicodeTranslateError | Raised when a unicode translation problem occurs                                  |
| ValueError            | Raised when there is a wrong value in a specified data type                       |
| ZeroDivisionError     | Raised when the second operator in a division is zero                             |

### Exception Handling

{% embed url="<https://www.youtube.com/watch?v=NIWwJbo-9_8>" %}

Python has many built-in exceptions that are raised when your program encounters an error (something in the program goes wrong).&#x20;

When these exceptions occur, the Python interpreter stops the current process and passes it to the calling process until it is handled. If not handled, the program will crash.

For example, let us consider a program where we have a function A that calls function B , which in turn calls function C. If an exception occurs in function C but is not handled in C, the exception passes to B and then to A .&#x20;

If never handled, an error message is displayed and our program comes to a sudden unexpected halt.

### Catching Exceptions in Python

&#x20;In Python, exceptions can be handled using a try statement.&#x20;

The critical operation which can raise an exception is placed inside the `try` clause. The code that handles the exceptions is written in the `except` clause.&#x20;

We can thus choose what operations to perform once we have caught the exception. Here is a simple example.

{% tabs %}
{% tab title="Python Code" %}

```python
# import module sys to get the type of exception
import sys

randomList = ['a', 0, 2]

for entry in randomList:
  try:
      print("The entry is", entry)
      r = 1/int(entry)
      break
  except:
      print("Oops!", sys.exc_info()[0], "occurred.")
      print("Next entry.")
      print()
print("The reciprocal of", entry, "is", r)
```

{% endtab %}

{% tab title="Output" %}

```python
The entry is a
Oops! <class 'ValueError'> occurred.
Next entry.

The entry is 0
Oops! <class 'ZeroDivisionError'> occured.
Next entry.

The entry is 2
The reciprocal of 2 is 0.5
```

{% endtab %}
{% endtabs %}

In this program, we loop through the values of the `randomList` list. As previously mentioned, the portion that can cause an exception is placed inside the try block.

If no exception occurs, the except block is skipped and normal flow continues(for last value). But if any exception occurs, it is caught by the except block (first and second values).&#x20;

Here, we print the name of the exception using the `exc_info()` function inside `sys` module. We can see that a causes `ValueError` and 0 causes `ZeroDivisionError` .

Since every exception in Python inherits from the base `Exception` class, we can also perform the above task in the following way:

<pre class="language-python"><code class="lang-python"># import module sys to get the type of exception
import sys

randomList = ['a', 0, 2]

for entry in randomList:
    try:
        print("The entry is", entry)
        r = 1/int(entry)
        break
    except Exception as e:
        print("Oops!", e.  class , "occurred.")
        print("Next entry.")
        print()
print("The reciprocal of", entry, "is", r)

<strong># This program has the same output as the above program
</strong></code></pre>

### Catching Specific Exceptions

In the above example, we did not mention any specific exception in the except clause.&#x20;

This is not a good programming practice as it will catch all exceptions and handle every case in the same way. We can specify which exceptions an except clause should catch.&#x20;

A `try` clause can have any number of except clauses to handle different exceptions, however, only one will be executed in case an exception occurs.&#x20;

We can use a tuple of values to specify multiple exceptions in an except clause. Here is an example pseudo code:

```python
try:
    # do something
    pass

except ValueError:
    # handle ValueError exception
    pass

except (TypeError, ZeroDivisionError):
    # handle multiple exceptions
    # TypeError and ZeroDivisionError
    pass

except:
    # handle all other exceptions
    pass
```

### Raising Exceptions in Python

In Python programming, exceptions are raised when errors occur at runtime. We can also manually raise exceptions using the `raise` keyword.&#x20;

We can optionally pass values to the exception to clarify why that exception was raised.

```python
>>> raise KeyboardInterrupt
Traceback (most recent call last):
 ...
KeyboardInterrupt

>>> raise MemoryError("This is an argument")
 Traceback (most recent call last):
 ...
 MemoryError: This is an argument

>>> try:
 ...	a = int(input("Enter a positive integer: "))
 ...	if a <= 0:
 ...	raise ValueError("That is not a positive number!")
 ... except ValueError as ve:
 ...	print(ve)
 ...
 Enter a positive integer: -2
 That is not a positive number!
```

### try with else clause

In some situations, you might want to run a certain block of code if the code block inside `try` ran without any errors. For these cases, you can use the optional `else` keyword with the `try` statement.

{% hint style="info" %}
Note: Exceptions in the else clause are not handled by the preceding except clauses.
{% endhint %}

Let's look at an example:

```python
# program to print the reciprocal of even numbers
try:
    num = int(input("Enter a number: ")) 
    assert num % 2 == 0
except:
    print("Not an even number!") 
else:
    reciprocal = 1/num 
    print(reciprocal)
```

#### Output

If we pass an odd number:

```python
Enter a number: 1
Not an even number!
```

If we pass an even number, the reciprocal is computed and displayed.

```python
Enter a number: 4
2 0.25
```

However, if we pass 0, we get `ZeroDivisionError` as the code block inside else is not handled by preceding except.

```python
Enter a number: 0
  Traceback (most recent call last):
    File "<string>", line 7, in <module>
     reciprocal = 1/num
  ZeroDivisionError: division by zero
```

### try...finally&#x20;

The `try` statement in Python can have an optional `finally` clause. This clause is executed no matter what, and is generally used to release external resources.

For example, we may be connected to a remote data center through the network or working with a file or a Graphical User Interface (GUI).

In all these circumstances, we must clean up the resource before the program comes to a halt whether it successfully ran or not. These actions (closing a file, GUI or disconnecting from network) are performed in the finally clause to guarantee the execution.&#x20;

Here is an example of file operations to illustrate this.

```python
try:
    f = open("test.txt",encoding = 'utf-8')
    # perform file operations
finally:
    f.close()
```

### Summary

After seeing the difference between syntax errors and exceptions, you learned about various ways to raise, catch, and handle exceptions in Python. Let's recap:

* `raise` allows you to throw an exception at any time.
* `assert` enables you to verify if a certain condition is met and throw an exception if it isn’t.
* In the `try` clause, all statements are executed until an exception is encountered.
* `except` is used to catch and handle the exception(s) that are encountered in the try clause.
* `else` lets you code sections that should run only when no exceptions are encountered in the try clause.
* `finally` enables you to execute sections of code that should always run, with or without any previously encountered exceptions.
