Heading image

Prerequisites

  1. Familiarity with Pipenv. See here for my post on Pipenv.
  2. Familiarity with JupyterLab. See here for my post on JupyterLab.

Getting started

Let’s create the hello-python-exceptions directory and install Pillow.

Creating the notebook

Once on http://localhost:8888/lab, select to create a new Python 3 notebook from the launcher.

  1. A demonstration on how try/except works.
  2. Looking at raising an error from a function.
  3. Creating and using custom errors.

Try/Except

We can use the try/except statement to handle errors. You raise an Exception whenever you want to throw an error from a code block.

Raising an error from a function

Defining a function that raises an error will traverse up the code block to the top level.

Creating and using custom errors

We can create custom errors simply by defining a class that extends Exception.

Summary

Today’s post demonstrated how to create custom errors by extending Exception and demonstrating how errors are raised in a simple manner.

Resources and further reading

  1. The ABCs of Pipenv
  2. Hello, JupyterLab
  3. Pipenv
  4. Python Exceptions — An Introduction
  5. User Defined Exceptions in Python

--

--

--

Senior Engineer @ UsabilityHub. Formerly Culture Amp.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Helpful sites to strengthen my coding skills!

Smallest Positive Missing Number in an Unsorted Array

OOP conecepts in Java

PostgreSQL - Backup & Point In Time Recovery

Implementing strStr() in Swift; breaking early

How to create temporary Email id with Gmail

L2Code: Getting A<head>

A/B Testing

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dennis O'Keeffe

Dennis O'Keeffe

Senior Engineer @ UsabilityHub. Formerly Culture Amp.

More from Medium

How To Publish Python Packages In PyPI In Two Simple Ways

Python-P01-Teach Me Python !

Making Interactive CLIs with Typer and Rich

Command line image

Finally ! Custom HTML parser coding concept has been developed in Python as well