By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,361 Members | 1,352 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,361 IT Pros & Developers. It's quick & easy.

How do I create user-defined warnings?

P: n/a
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
Share this Question
Share on Google+
1 Reply

P: n/a
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.