469,907 Members | 2,170 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,907 developers. It's quick & easy.

How do I create user-defined warnings?

I already know how to make user-defined exceptions, like this one:

class MyException(Exception):
pass

But for a module I'm making, I would like to make a warning (so it just
prints the warning to stderr and doesn't crash the program). I have
tried this:

class MyWarning(Warning):
pass

And it behaves like a normal error. Please help me, I can't figure out
what I'm doing wrong.

--
Ratfink

Jun 27 '08 #1
1 1062
Clay Hobbs wrote:
I already know how to make user-defined exceptions, like this one:

class MyException(Exception):
pass

But for a module I'm making, I would like to make a warning (so it just
prints the warning to stderr and doesn't crash the program). I have
tried this:

class MyWarning(Warning):
pass

And it behaves like a normal error. Please help me, I can't figure out
what I'm doing wrong.
Are you using the warning with 'raise'? Don't do that, use warnings.warn instead:

In [1]: import warnings

In [2]: class MyWarning(Warning): pass
...:

In [3]: warnings.warn(MyWarning("bah humbug"))
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/IPython/FakeModule.py:1:
MyWarning: bah humbug
# -*- coding: utf-8 -*-

--
Hans Nowak (zephyrfalcon at gmail dot com)
http://4.flowsnake.org/
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Keith Jakobs, MCP | last post: by
reply views Thread by Renato Neves | last post: by
10 posts views Thread by ibm_97 | last post: by
4 posts views Thread by I_AM_DON_AND_YOU? | last post: by
reply views Thread by richb330 | last post: by
6 posts views Thread by Philip Wagenaar | last post: by
5 posts views Thread by Michael | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.