Proper way to handle errors in a module

Prasad, Ramit ramit.prasad at jpmchase.com
Wed May 11 14:50:00 EDT 2011


The simple but code heavy system is to create two functions. One that raises an error and one that returns None or something that is mutually agreed to be invalid.  You can even get away with one of them doing the actual work and the other one just as a wrapper. I feel too lazy to fix the mistakes below, but you get the idea.

 Def a():
	Blahblah
	Raise SomeError

Def b():
	Try:
		A()
	Except SomeError:
		Return blah


Ramit



Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423


-----Original Message-----
From: python-list-bounces+ramit.prasad=jpmchase.com at python.org [mailto:python-list-bounces+ramit.prasad=jpmchase.com at python.org] On Behalf Of Andrew Berg
Sent: Wednesday, May 11, 2011 1:15 PM
To: python-list at python.org
Subject: Re: Proper way to handle errors in a module

On 2011.05.11 12:57 PM, Patty wrote:
> Hi Andrew -
>
> Sometimes you want an exception come up and then use that information to 
> take your
> program in some direction.
Right, but I'm wondering how I should handle errors in a module, where
different people will want their programs to do different things when an
error occurs in a function or method from my module (which is not
necessarily because of a bug in my module - the calling script could
send a bad parameter or some system configuration could prevent the
function from doing something). On one hand, I could let almost all
exceptions go unhandled and have the calling script handle them. This
would force the developer of the calling script to learn all the
exceptions that my module could raise (which could be really annoying if
I have custom exceptions for problems that don't otherwise raise an
exception). OTOH, I could handle all the exceptions and return None or
some default value, but that would make it harder for the developer to
make the script react appropriately.
-- 
http://mail.python.org/mailman/listinfo/python-list
This communication is for informational purposes only. It is not
intended as an offer or solicitation for the purchase or sale of
any financial instrument or as an official confirmation of any
transaction. All market prices, data and other information are not
warranted as to completeness or accuracy and are subject to change
without notice. Any comments or statements made herein do not
necessarily reflect those of JPMorgan Chase & Co., its subsidiaries
and affiliates.

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law. If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED. Although this transmission and any
attachments are believed to be free of any virus or other defect
that might affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by JPMorgan Chase &
Co., its subsidiaries and affiliates, as applicable, for any loss
or damage arising in any way from its use. If you received this
transmission in error, please immediately contact the sender and
destroy the material in its entirety, whether in electronic or hard
copy format. Thank you.

Please refer to http://www.jpmorgan.com/pages/disclosures for
disclosures relating to European legal entities.



More information about the Python-list mailing list