1. Computing

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

  1. About.com
  2. Computing
  3. Python
  4. Beginning Python
  5. Python Exceptions - Raising Your Own Exceptions With raise - Part 2

©2013 About.com. All rights reserved.