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

First Experience with Using Scrum

How to gain peace of mind with a managed digital workplace?

Going beyond with Postman

Why to Integrate Uber API: Benefits and Successful Cases

Creating a database using superhero “Python”

Coding Standard in Java

The Boy Scout Rule in Coding

LeetCode Patterns Adventure 21 — Minimum Depth of Binary Tree

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

Nested loop part-2

INSERTION OF DATA AT THE BEGINNING, AFTER A NODE AND ENDING POSITION OF A LINKLIST IN PYTHON…

Capture Date, Phone and Email From Text With Regular Expression in Python

How to cache a method of an unhashable type in Python?