{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<small><small><i>\n",
    "All the IPython Notebooks in **Python Files** lecture series by **[Dr. Milaan Parmar](https://www.linkedin.com/in/milaanparmar/)** are available @ **[GitHub](https://github.com/milaan9/05_Python_Files)**\n",
    "</i></small></small>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python Errors and Built-in Exceptions\n",
    "\n",
    "In this class, you will learn about different types of errors and exceptions that are built-in to Python. They are raised whenever the Python interpreter encounters errors.\n",
    "\n",
    "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:\n",
    "\n",
    "1. Syntax errors\n",
    "2. Logical errors (Exceptions)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Python Syntax Errors\n",
    "\n",
    "Error caused by not following the proper structure (syntax) of the language is called **syntax error** or **parsing error**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-18T15:39:26.584572Z",
     "start_time": "2021-06-18T15:39:26.566021Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-1-a8dfd56d4d8e>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-1-a8dfd56d4d8e>\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m    if a < 3\u001b[0m\n\u001b[1;37m            ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# Example 1:\n",
    "\n",
    "if a < 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exaplanation:**\n",
    "\n",
    "As shown in the example, an arrow indicates where the parser ran into the syntax error.\n",
    "\n",
    "We can notice here that a colon **`:`** is missing in the **`if`** statement."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "Missing parentheses in call to 'print'. Did you mean print('hello world')? (<ipython-input-3-cb8ac02fbcd3>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-3-cb8ac02fbcd3>\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m    print 'hello world'\u001b[0m\n\u001b[1;37m          ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m Missing parentheses in call to 'print'. Did you mean print('hello world')?\n"
     ]
    }
   ],
   "source": [
    "# Example 2:\n",
    "\n",
    "print 'hello world'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exaplanation:**\n",
    "\n",
    "As you can see we made a syntax error because we forgot to enclose the string with parenthesis **`()`** and Python already suggests the solution. Let us fix it."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Python Logical Errors (Exceptions)\n",
    "\n",
    "Errors that occur at runtime (after passing the syntax test) are called **exceptions** or **logical errors**.\n",
    "\n",
    "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`**).\n",
    "\n",
    "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.\n",
    "\n",
    "Let's look at how Python treats these errors:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-18T15:39:28.907309Z",
     "start_time": "2021-06-18T15:39:28.612394Z"
    }
   },
   "outputs": [
    {
     "ename": "ZeroDivisionError",
     "evalue": "division by zero",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mZeroDivisionError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-4-9b9247988596>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# Example 1: ZeroDivisionError\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;36m1\u001b[0m \u001b[1;33m/\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mZeroDivisionError\u001b[0m: division by zero"
     ]
    }
   ],
   "source": [
    "# Example 1: ZeroDivisionError\n",
    "\n",
    "1 / 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-18T15:39:32.662651Z",
     "start_time": "2021-06-18T15:39:32.644100Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'imaginary.txt'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-5-6ac8e81b6f0c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# Example 2: FileNotFoundError\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"imaginary.txt\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'imaginary.txt'"
     ]
    }
   ],
   "source": [
    "# Example 2: FileNotFoundError\n",
    "\n",
    "open(\"imaginary.txt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-18T15:39:32.662651Z",
     "start_time": "2021-06-18T15:39:32.644100Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "cannot import name 'power' from 'math' (unknown location)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-0ef33cc3a94d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# Example 3: ImportError\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mmath\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mpower\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m: cannot import name 'power' from 'math' (unknown location)"
     ]
    }
   ],
   "source": [
    "# Example 3: ImportError\n",
    "\n",
    "from math import power"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exaplanation:**\n",
    "\n",
    "There is no function called **`power`** in the **`math`** module, it goes with a different name: **`pow`**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Python NameError\n",
    "\n",
    "We debugged the error by defining the variable name."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-18T15:39:28.907309Z",
     "start_time": "2021-06-18T15:39:28.612394Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'age' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-7-119e50d96cfb>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# Example 1:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mage\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'age' is not defined"
     ]
    }
   ],
   "source": [
    "# Example 1:\n",
    "\n",
    "print(age)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exaplanation:**\n",
    "\n",
    "As you can see from the message above, name **`age`** is not defined. Yes, it is true that we did not define an **`age`** variable but we were trying to print it out as if we had had declared it. Now, lets fix this by declaring it and assigning with a value."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Python IndexError\n",
    "\n",
    "We debugged the error by defining the variable name."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-18T15:39:28.907309Z",
     "start_time": "2021-06-18T15:39:28.612394Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-8-c00e5d7d3c3e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mnumbers\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m5\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mnumbers\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "# Example 1:\n",
    "\n",
    "numbers = [1, 2, 3, 4, 5]\n",
    "numbers[5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exaplanation:**\n",
    "\n",
    "In the example above, Python raised an **`IndexError`**, because the list has only indexes from 0 to 4 , so it was out of range."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Python ModuleNotFoundError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-18T15:39:28.907309Z",
     "start_time": "2021-06-18T15:39:28.612394Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'maths'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-9-e2e604bf92de>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# Example 1:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mmaths\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'maths'"
     ]
    }
   ],
   "source": [
    "# Example 1:\n",
    "\n",
    "import maths"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exaplanation:**\n",
    "\n",
    "In the example above, I added an extra **`s`** to math deliberately and **`ModuleNotFoundError`** was raised. Lets fix it by removing the extra **`s`** from math."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. Python AttributeError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-18T15:39:28.907309Z",
     "start_time": "2021-06-18T15:39:28.612394Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'math' has no attribute 'PI'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-10-df6ce7ede1dc>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mmath\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mmath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mPI\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: module 'math' has no attribute 'PI'"
     ]
    }
   ],
   "source": [
    "# Example 1:\n",
    "\n",
    "import math\n",
    "math.PI"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exaplanation:**\n",
    "\n",
    "As you can see, I made a mistake again! Instead of **`pi`**, I tried to call a **`PI`** function from **`math`** module. It raised an **`AttributeError`**, it means, that the function does not exist in the module. Lets fix it by changing from **`PI`** to **`pi`**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7. Python KeyError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-18T15:39:28.907309Z",
     "start_time": "2021-06-18T15:39:28.612394Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Milaan'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Example 1:\n",
    "\n",
    "users = {'name':'Milaan', 'age':96, 'country':'England'}\n",
    "users['name']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'county'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-12-fbf7cddd4b2a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0musers\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'county'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 'county'"
     ]
    }
   ],
   "source": [
    "users['county']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exaplanation:**\n",
    "\n",
    "As you can see, there was a typo in the key used to get the dictionary value. so, this is a **`KeyError`** and the fix is quite straight forward. Let's do this!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8. Python TypeError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-18T15:39:28.907309Z",
     "start_time": "2021-06-18T15:39:28.612394Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for +: 'int' and 'str'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-13-f7ffd6347533>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# Example 1:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;36m6\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;34m'3'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'"
     ]
    }
   ],
   "source": [
    "# Example 1:\n",
    "\n",
    "6 + '3'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exaplanation:**\n",
    "\n",
    "In the example above, a **`TypeError`** is raised because we cannot add a number to a string. First solution would be to convert the string to **int** or **float**. Another solution would be converting the number to a string (the result then would be '63'). Let us follow the first fix."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9. Python ValueError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-18T15:39:28.907309Z",
     "start_time": "2021-06-18T15:39:28.612394Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: '19a'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-14-ee4e8ee8c904>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# Example 1:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'19a'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: '19a'"
     ]
    }
   ],
   "source": [
    "# Example 1:\n",
    "\n",
    "int('19a')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exaplanation:**\n",
    "\n",
    "In this case we cannot change the given string to a number, because of the **`a`** letter in it."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python Built-in Exceptions\n",
    "\n",
    "Illegal operations can raise exceptions. There are plenty of built-in exceptions in Python that are raised when corresponding errors occur. We can view all the built-in exceptions using the built-in **`local()`** function as follows:\n",
    "\n",
    "```python\n",
    "print(dir(locals()['__builtins__']))\n",
    "```\n",
    "\n",
    "**`locals()['__builtins__']`** will return a module of built-in exceptions, functions, and attributes. **`dir`** allows us to list these attributes as strings.\n",
    "\n",
    "Some of the common built-in exceptions in Python programming along with the error that cause them are listed below:\n",
    "\n",
    "| Exception | Cause of Error |\n",
    "|:----| :--- |\n",
    "| **`AssertionError`** |   Raised when an **`assert`** statement fails.   | \n",
    "| **`AttributeError`** |   Raised when attribute assignment or reference fails.   | \n",
    "| **`EOFError`** |   Raised when the **`input()`** function hits end-of-file condition.   | \n",
    "| **`FloatingPointError`** |   Raised when a floating point operation fails.   | \n",
    "| **`GeneratorExit`** |   Raise when a generator's **`close()`** method is called.   | \n",
    "| **`ImportError`** |   Raised when the imported module is not found.   | \n",
    "| **`IndexError`** |   Raised when the index of a sequence is out of range.   | \n",
    "| **`KeyError`** |   Raised when a key is not found in a dictionary.   | \n",
    "| **`KeyboardInterrupt`** |   Raised when the user hits the interrupt key (**`Ctrl+C`** or **`Delete`**).   | \n",
    "| **`MemoryError`** |   Raised when an operation runs out of memory.   | \n",
    "| **`NameError`** |   Raised when a variable is not found in local or global scope.   | \n",
    "| **`NotImplementedError`** |   Raised by abstract methods.   | \n",
    "| **`OSError`** |   Raised when system operation causes system related error.   | \n",
    "| **`OverflowError`** |   Raised when the result of an arithmetic operation is too large to be represented.   | \n",
    "| **`ReferenceError`** |   Raised when a weak reference proxy is used to access a garbage collected referent.   | \n",
    "| **`RuntimeError`** |   Raised when an error does not fall under any other category.   | \n",
    "| **`StopIteration`** |   Raised by **`next()`** function to indicate that there is no further item to be returned by iterator.   | \n",
    "| **`SyntaxError`** |   Raised by parser when syntax error is encountered.   | \n",
    "| **`IndentationError`** |   Raised when there is incorrect indentation.   | \n",
    "| **`TabError`** |   Raised when indentation consists of inconsistent tabs and spaces.   | \n",
    "| **`SystemError`** |   Raised when interpreter detects internal error.   | \n",
    "| **`SystemExit`** |   Raised by **`sys.exit()`** function.   | \n",
    "| **`TypeError`** |   Raised when a function or operation is applied to an object of incorrect type.   | \n",
    "| **`UnboundLocalError`** |   Raised when a reference is made to a local variable in a function or method, but no value has been bound to that variable.   | \n",
    "| **`UnicodeError`** |   Raised when a Unicode-related encoding or decoding error occurs.   | \n",
    "| **`UnicodeEncodeError`** |   Raised when a Unicode-related error occurs during encoding.   | \n",
    "| **`UnicodeDecodeError`** |   Raised when a Unicode-related error occurs during decoding.   | \n",
    "| **`UnicodeTranslateError`** |   Raised when a Unicode-related error occurs during translating.   | \n",
    "| **`ValueError`** |   Raised when a function gets an argument of correct type but improper value.   | \n",
    "| **`ZeroDivisionError`** |   Raised when the second operand of division or modulo operation is zero.   | "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If required, we can also define our own exceptions in Python. To learn more about them, visit Python **[User-defined Exceptions](https://github.com/milaan9/05_Python_Files/blob/main/005_Python_User_defined_Exceptions.ipynb)**.\n",
    "\n",
    "We can handle these built-in and user-defined exceptions in Python using **`try`**, **`except`** and **`finally`** statements. To learn more about them, visit **[Python try, except and finally statements](https://github.com/milaan9/05_Python_Files/blob/main/004_Python_Exceptions_Handling.ipynb)**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 💻 Exercises ➞ <span class='label label-default'>Python Errors</span>\n",
    "\n",
    "1. Open you Jupyter Notebook and try all the examples covered in this section."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}