"Cell \u001b[0;32mIn[4], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mmy_func\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m\n",
"Cell \u001b[0;32mIn[2], line 4\u001b[0m, in \u001b[0;36mmy_func\u001b[0;34m(a, b)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\" what can go wrong with this function? \"\"\"\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m a \u001b[38;5;241m>\u001b[39m b:\n\u001b[0;32m----> 4\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43ma\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m a \u001b[38;5;241m*\u001b[39m c\n",
"Cell \u001b[0;32mIn[4], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mmy_func\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m)\u001b[49m\n",
"Cell \u001b[0;32mIn[1], line 6\u001b[0m, in \u001b[0;36mmy_func\u001b[0;34m(a, b)\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m a \u001b[38;5;241m/\u001b[39m b\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m----> 6\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m a \u001b[38;5;241m*\u001b[39m \u001b[43mc\u001b[49m\n",
"\u001b[0;31mNameError\u001b[0m: name 'c' is not defined"
]
}
],
"source": [
"my_func(2, 10)"
]
},
{
"cell_type": "markdown",
"id": "b7a97b68-43a1-4c10-8621-284d434b12a9",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 7,
"id": "1191e7d1-4037-47d7-a899-4f0c0d6846ff",
"metadata": {
"tags": []
},
"outputs": [
{
"ename": "TypeError",
"evalue": "'>' not supported between instances of 'int' and 'str'",
"Cell \u001b[0;32mIn[8], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mmy_func\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mb\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43ma\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
"Cell \u001b[0;32mIn[1], line 3\u001b[0m, in \u001b[0;36mmy_func\u001b[0;34m(a, b)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mmy_func\u001b[39m(a, b):\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m a \u001b[38;5;241m>\u001b[39m b:\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43ma\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m a \u001b[38;5;241m*\u001b[39m c\n",
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for /: 'str' and 'str'"
]
}
],
"source": [
"my_func(\"b\", \"a\")"
]
},
{
"cell_type": "markdown",
"id": "6c8c321f-0a0f-4116-94e0-cb91421c1492",
"metadata": {},
"source": [
"We could try to think of all possible conditions, and our simple function would become much longer & harder to read. \n",
"\n",
"Often there is no better solution than to have an error occur, so Python **raises an exception** and that is what happens here."
]
},
{
"cell_type": "markdown",
"id": "0877c7a7-a636-47a1-a5d2-05ff8e5bb920",
"metadata": {},
"source": [
"The above types are called rumtime exceptions, because we don't see them until the code runs.\n",
"\n",
"This is different from a `SyntaxError` which means that Python can't parse our statements:\n",
"\n",
"```\n",
"# Syntax Errors\n",
"def my_func(a, b,: # missing )\n",
" if a > b\n",
" return a / b # missing colon\n",
" else { return a * c } # python doesn't use braces\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "14c7426d-9b73-4712-b204-db9cd6bcde67",
"metadata": {},
"source": [
"## Built-in Exceptions\n",
"\n",
"* `Exception` (base type)\n",
" * `ValueError`\n",
" * `TypeError`\n",
" * `KeyError`\n",
" * `IndexError`\n",
" * `NotImplementedError`\n",
" * `OSError`\n",
" * `FileNotFoundError`\n",
"\n",
"And many more: https://docs.python.org/3/library/exceptions.html#exception-hierarchy\n",
"\n",
"Note that exceptions form a hierarchy, we'll discuss this in more detail when we come to inheritance.\n",
"\n",
"## Handling Exceptions\n",
"\n",
"In practice, exceptions are **raised**, and either **caught** or not. An uncaught exception stops the program and prints an error message to the screen as you've seen.\n",
"Cell \u001b[0;32mIn[5], line 5\u001b[0m\n\u001b[1;32m 3\u001b[0m b \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;66;03m#c = a / b\u001b[39;00m\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43md\u001b[49m)\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mlast line of try\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mZeroDivisionError\u001b[39;00m:\n",
"\u001b[0;31mNameError\u001b[0m: name 'd' is not defined"