{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic exception handling" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "An exception has occurred\n" ] } ], "source": [ "class Calc:\n", " def Inverse(self, num):\n", " return 1/num\n", "\n", "try:\n", " cal = Calc()\n", " cal.Inverse(0)\n", "except Exception as err:\n", " print(\"An exception has occurred\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Handling specific exceptions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The exception in below example will be caught by first except block" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A zero division exception has occurred. Exception message - division by zero\n" ] } ], "source": [ "try:\n", " cal = Calc()\n", " cal.Inverse(0)\n", "except ZeroDivisionError as zeroDivideError:\n", " print(\"A zero division exception has occurred. Exception message -\", zeroDivideError)\n", "except Exception as err:\n", " print(\"An exception has occurred -\", err)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The exception in below example will be caught by second except block" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "An exception has occurred - object() takes no parameters\n" ] } ], "source": [ "try:\n", " cal = Calc(\"\")\n", " cal.Inverse(0)\n", "except ZeroDivisionError as zeroDivideError:\n", " print(\"A zero division exception has occurred. Exception message -\", zeroDivideError)\n", "except Exception as err:\n", " print(\"An exception has occurred -\", err)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Raising an exception" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "An exception has occurred - Age can not be less than 18\n" ] } ], "source": [ "class Student:\n", " def setAge(self, age):\n", " if(age < 18):\n", " raise Exception(\"Age can not be less than 18\")\n", " else:\n", " self.age = age\n", "\n", "alice = Student()\n", "try:\n", " alice.setAge(15)\n", "except Exception as error:\n", " print(\"An exception has occurred -\", error)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# The else and finally block" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The else block" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Age was assigned successfully!\n" ] } ], "source": [ "bob = Student()\n", "try:\n", " bob.setAge(20)\n", "except Exception as error:\n", " print(\"An exception has occurred -\", error)\n", "else:\n", " print(\"Age was assigned successfully!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The finally block" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Age was assigned successfully!\n", "Finally block - This always gets executed\n" ] } ], "source": [ "bob = Student()\n", "try:\n", " bob.setAge(20)\n", "except Exception as error:\n", " print(\"An exception has occurred -\", error)\n", "else:\n", " print(\"Age was assigned successfully!\")\n", "finally:\n", " print(\"Finally block - This always gets executed\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# User defined exceptions" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Salary exception occurred - Salary can't be less than 0\n" ] } ], "source": [ "class SalaryError(Exception):\n", " def __init__(self, arg):\n", " self.arg = arg\n", "\n", "class Employee:\n", " def setSalary(self, salary):\n", " if(salary < 0):\n", " raise SalaryError(\"Salary can't be less than 0\")\n", " else:\n", " self.salary = salary\n", "\n", "try:\n", " chris = Employee()\n", " chris.setSalary(-100)\n", "except SalaryError as error:\n", " print(\"Salary exception occurred - \", error)" ] } ], "metadata": { "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.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }