• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Error Handling / Python’s Built-In Exceptions

Python’s Built-In Exceptions

Author: PFB Staff Writer
Last Updated: May 25, 2020

Python’s Built-In Exceptions

BaseException

The base class for all built-in exceptions.

Exception

All built-in, non-system-exiting exceptions are derived from this class.

All user-defined exceptions should also be derived from this class.

StandardError

The base class for all built-in exceptions except StopIteration, GeneratorExit,
KeyboardInterrupt and SystemExit. StandardError itself is derived fromException.

ArithmeticError

The base class for those built-in exceptions that are raised for various
arithmetic errors: OverflowError, ZeroDivisionError, FloatingPointError

LookupError

The base class for the exceptions that are raised when a key or index used on
a mapping or sequence is invalid: IndexError, KeyError.

This can be raised directly by sys.setdefaultencoding()

EnvironmentError

The base class for exceptions that can occur outside the Python system:
IOError, OSError.

AssertionError

Raised when an assert statement fails.

AttributeError

Raised when an attribute reference or assignment fails.

EOFError

Raised when one of the built-in functions (input() or raw_input()) hits an
end-of-file condition (EOF) without reading any data.

FloatingPointError

Raised when a floating point operation fails.

GeneratorExit

Raise when a generator’s close() method is called.

It directly inherits from Exception instead of StandardError since it is
technically not an error.

IOError

Raised when an I/O operation (such as a print statement, the built-in open()
function or a method of a file object) fails for an I/O-related reason,
e.g., “file not found” or “disk full”.

This class is derived from EnvironmentError.

ImportError

Raised when an import statement fails to find the module definition or when a
from…import fails to find a name that is to be imported.

IndexError

Raised when a sequence subscript is out of range.

KeyError

Raised when a mapping (dictionary) key is not found in the set of existing keys.

KeyboardInterrupt

Raised when the user hits the interrupt key (normally Control-C or Delete).

MemoryError

Raised when an operation runs out of memory but the situation may still be
rescued (by deleting some objects).

NameError

Raised when a local or global name is not found.

This applies only to unqualified names.

The associated value is an error message that includes the name that could
not be found.

NotImplementedError

This exception is derived from RuntimeError.

In user defined base classes, abstract methods should raise this exception when
they require derived classes to override the method.

OSError

This class is derived from EnvironmentError and is used primarily as the
os module’s os.error exception.

OverflowError

Raised when the result of an arithmetic operation is too large to be represented.

ReferenceError

This exception is raised when a weak reference proxy, created by the
weakref.proxy() function, is used to access an attribute of the referent
after it has been garbage collected.

RuntimeError

Raised when an error is detected that doesn’t fall in any of the other categories.

StopIteration:

Raised by an iterator’s next() method to signal that there are no further values.

SyntaxError

Raised when the parser encounters a syntax error.

SystemError

Raised when the interpreter finds an internal error,
but the situation does not look so serious to cause it to abandon all hope.

The associated value is a string indicating what went wrong (in low-level terms).

SystemExit

This exception is raised by the sys.exit() function.

When it is not handled, the Python interpreter exits; no stack traceback is
printed.

If the associated value is a plain integer, it specifies the system exit status
(passed to C’s exit() function) ; if it is None, the exit status is zero;
if it has another type (such as a string), the object’s value is printed and
the exit status is one.

TypeError

Raised when an operation or function is applied to an object of inappropriate
type.

The associated value is a string giving details about the type mismatch.

UnboundLocalError

Raised when a reference is made to a local variable in a function or method,
but no value has been bound to that variable.

UnicodeDecodeError

Raised when a Unicode-related encoding or decoding error occurs.

It is a subclass of ValueError.

UnicodeEncodeError

Raised when a Unicode-related error occurs during encoding.

It is a subclass of UnicodeError.

UnicodeError

Raised when a Unicode-related error occurs during decoding.

It is a subclass of UnicodeError.

UnicodeTranslateError

Raised when a Unicode-related error occurs during translating.

It is a subclass of UnicodeError.

ValueError

Raised when a built-in operation or function receives an argument that has the
right type but an inappropriate value, and the situation is not described by a
more precise exception such as IndexError.

WindowsError

Raised when a Windows-specific error occurs or when the error number does not
correspond to an error value.

ZeroDivisionError

Raised when the second argument of a division or modulo operation is zero.

The associated value is a string indicating the type of the operands and the
operation.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Error Handling Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary โ€“ How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 ยท PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us