Member-only story
Beginner Guide to Exception and Exception Handling in Python
Exception handling in Python is done by using the try except clause
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…