Member-only story

Beginner Guide to Exception and Exception Handling in Python

Exception handling in Python is done by using the try except clause

billydharmawan
TDS Archive
8 min readMay 7, 2020

--

Photo by Hitesh Choudhary on Unsplash

Introduction

In this tutorial, we will learn how to handle exceptions in Python by using the try except clause.

But first, what is an exception? 🤔

An exception is an error that is thrown by our code when the execution of the code results in an unexpected outcome. Normally, an exception will have an error type and an error message. Some examples are as follows.

ZeroDivisionError: division by zero
TypeError: must be str, not int

ZeroDivisionError and TypeError are the error type and the text that comes after the colon is the error message. The error message usually describes the error type.

Every good code and application handles exceptions to keep the user experience smooth and seamless. So, let’s learn how to do that, shall we? 🙂

Built-in Exceptions

In Python, all the built-in exceptions are derived from the BaseException class. The exception classes that directly inherit the BaseException class are: Exception, GeneratorExit, KeyboardInterrupt and SystemExit. We are going to focus on the Exception class in…

--

--

TDS Archive
TDS Archive

Published in TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

billydharmawan
billydharmawan

Written by billydharmawan

An ordinary man trying to leave a good legacy on earth

No responses yet