Stats

496 Contributors: 26 Friday, August 4, 2017
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap

Exceptions

Introduction

Errors detected during execution are called exceptions and are not unconditionally fatal. Most exceptions are not handled by programs; it is possible to write programs that handle selected exceptions. There are specific features in Python to deal with exceptions and exception logic. Furthermore, exceptions have a rich type hierarchy, all inheriting from the BaseException type.

Syntax

  • raise exception
  • raise # re-raise an exception that’s already been raised
  • raise exception from cause # Python 3 - set exception cause
  • raise exception from None # Python 3 - suppress all exception context
  • try:
  • except [exception types] [ as identifier ]:
  • else:
  • finally:

Related Examples