1. Home
  2. Computing & Technology
  3. Python

Beginning Python: Exceptions, Errors, and Warnings

From , former About.com Guide

8 of 8

Raising Your Own Exceptions With raise - Part 2

But what if you do not want to give the user something obtuse like NameError or IOError. Python also provides a way to present customised error messages by using the built-in class Exception. To use it, simply plug "Exception" as the argument of raise.

>>> raise Exception
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Exception

"Where is the error message?" Well, I wanted you to see that you could raise a simple error by not giving any further arguments than "Exception". If you want to print something beyond this, enclose your message in quotes and separate it from "Exception" by a comma:
>>> raise Exception, "This is my customised error message."
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Exception: This is my customised error message.

That summarises Python's exceptions, if you would like a list of them, you will find a page each of Python's exceptions and warnings in the section on the Python Library.

Other tutorials in this series: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8

Explore Python
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Python

©2009 About.com, a part of The New York Times Company.

All rights reserved.