Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'exception': 0.03; 'argument': 0.04; 'handler': 0.04; 'interpreter': 0.04; 'except:': 0.07; 'exception.': 0.07; 'suppose': 0.07; 'try:': 0.07; 'defined.': 0.09; 'handlers': 0.09; 'here?': 0.09; 'def': 0.10; 'read.': 0.13; '"manual"': 0.16; 'bypassing': 0.16; 'error"': 0.16; 'occurs,': 0.16; 'possible?': 0.16; 'subject:arithmetic': 0.16; 'wrote:': 0.17; 'appropriate': 0.20; 'define': 0.20; 'do.': 0.21; 'exceptions': 0.22; 'idea': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'creating': 0.26; 'converting': 0.27; 'prints': 0.29; 'search.': 0.29; 'function': 0.30; 'error': 0.30; 'code': 0.31; 'could': 0.32; 'print': 0.32; 'cases,': 0.33; 'like:': 0.33; 'handle': 0.33; 'to:addr:python-list': 0.33; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'possible': 0.37; 'bad': 0.37; 'passed': 0.37; 'subject:: ': 0.38; 'mark': 0.38; 'some': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'your': 0.60; 'most': 0.61; 'kind': 0.61; 'received:204': 0.72; 'special': 0.73; '666': 0.84; 'here...': 0.84; 'difficult,': 0.91; 'do:': 0.91; 'prone': 0.91 Date: Thu, 23 Aug 2012 11:23:20 +0200 From: Laszlo Nagy User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Guarding arithmetic References: <8b9a5844-66b0-4940-946a-5e626462cdce@googlegroups.com> In-Reply-To: <8b9a5844-66b0-4940-946a-5e626462cdce@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345713811 news.xs4all.nl 6874 [2001:888:2000:d::a6]:47104 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27714 On 2012-08-23 11:05, Mark Carter wrote: > Suppose I want to define a function "safe", which returns the argument passed if there is no error, and 42 if there is one. So the setup is something like: > > def safe(x): > # WHAT WOULD DEFINE HERE? > > print safe(666) # prints 666 > print safe(1/0) # prints 42 > > I don't see how such a function could be defined. Is it possible? You are very vague. "There is an error" - but what kind of error? To catch all possible exceptions you could do: def unsafe(x): # put your code here... def safe(x): try: return unsafe(x) except: return 42 Generally, it is a bad idea. Exception handlers were invented because they give you a way to handle any error in the call chain. When an exception occurs, the interpreter will start searching for an appropriate exception handler traversing up in the call chain. By converting exceptions into return values, you are bypassing this search. Then you will have to write conditions instead of exception handlers inside ALL methods in the call chain, creating a "manual" search for the handler of the exception. In most cases, this will make your code difficult, error prone and hard to read. In some special cases, this can be a good idea to do. Can you please let us know when and how would you like to use it?