473,785 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

converting a nested try/except statement into try/except/else

I'm starting out with this:

try:
if int(text) 0:
return True
else:
self.error_mess age()
return False
except ValueError:
self.error_mess age()
return False

I rewrote it as this:

try:
int(text)
except ValueError:
self.error_mess age()
return False
else:
return True

I think it's much cleaner, but obviously I lost the test in the original
if statement.

So my question is, can I still retain this second structure and still
test for 0, but not have any extra nesting?

Thanks.
Aug 9 '06 #1
20 3927
John Salerno a écrit :
I'm starting out with this:

try:
if int(text) 0:
return True
else:
self.error_mess age()
return False
except ValueError:
self.error_mess age()
return False

I rewrote it as this:

try:
int(text)
except ValueError:
self.error_mess age()
return False
else:
return True

I think it's much cleaner, but obviously I lost the test in the original
if statement.

So my question is, can I still retain this second structure and still
test for 0, but not have any extra nesting?
solution 1:

def wrong():
raise ValueError

try:
int(text) 0 or wrong()
except ValueError:
self.error_mess age()
return False
else:
return True

But that's being-too-clever imho...

solution 2:

def error_message() :
self.error_mess age()
return False

try:
return int(text) 0 or error_message()
except ValueError:
return error_message()

Aug 9 '06 #2
John Salerno wrote:
I'm starting out with this:

try:
if int(text) 0:
return True
else:
self.error_mess age()
return False
except ValueError:
self.error_mess age()
return False

I rewrote it as this:

try:
int(text)
except ValueError:
self.error_mess age()
return False
else:
return True

I think it's much cleaner, but obviously I lost the test in the original
if statement.

So my question is, can I still retain this second structure and still
test for 0, but not have any extra nesting?

Thanks.
What about the version I gave you 8 days ago? ;-)

http://groups.google.ca/group/comp.l...0fcd8932b0733a

It's clean, does the job, and doesn't have any extra nesting.

Peace,
~Simon

Aug 9 '06 #3
Bruno Desthuilliers a écrit :
John Salerno a écrit :
(snip)

or of course the dead simple:

try:
if int(text) <= 0: raise ValueError
except ValueError:
self.error_mess age()
return False
else:
return True
BTW, you really should have a look at FormEncode...
Aug 9 '06 #4
Bruno Desthuilliers wrote:
try:
if int(text) <= 0: raise ValueError
except ValueError:
self.error_mess age()
return False
else:
return True
Nice! Thanks!
Aug 10 '06 #5
Simon Forman wrote:
What about the version I gave you 8 days ago? ;-)

http://groups.google.ca/group/comp.l...0fcd8932b0733a

It's clean, does the job, and doesn't have any extra nesting.

Peace,
~Simon
I remember that version, but I found it a little hard to follow. It
seems like the kind of code that if I look at it again in another month
or so, I'll have to trace through it again to figure out what's what.

But I think it was your code that made me think of using an else
statement in the first place! :)
Aug 10 '06 #6
Bruno Desthuilliers wrote:
try:
if int(text) <= 0: raise ValueError
Hmm, I'm actually not so sure about this line now. It doesn't seem right
to raise a ValueError when the result of the expression is negative,
because even though it's a problem for my program, it isn't really a
"ValueError ," right?
Aug 10 '06 #7
On Wed, 09 Aug 2006 18:51:04 +0000 (GMT)
John Salerno <jo******@NOSPA Mgmail.comwrote :

#try:
# int(text)
#except ValueError:
# self.error_mess age()
# return False
#else:
# return True
#>
#I think it's much cleaner, but obviously I lost the test in the
#original if statement.
#>
#So my question is, can I still retain this second structure and
#still test for 0, but not have any extra nesting?

How about

try:
if int(text) 0:
return True
except ValueError:
pass
self.error_mess age()
return False

--
Best wishes,
Slawomir Nowaczyk
( Sl************* **@cs.lth.se )

In 10 minutes, a hurricane releases more energy than all of the world's
nuclear weapons combined.

Aug 10 '06 #8
try:
if int(text) <= 0: raise ValueError

Hmm, I'm actually not so sure about this line now. It doesn't seem right
to raise a ValueError when the result of the expression is negative,
because even though it's a problem for my program, it isn't really a
"ValueError ," right?
It's an invalid value to your program, so yes, it is a ValueError.

Aug 10 '06 #9
John Salerno a écrit :
Bruno Desthuilliers wrote:
> try:
if int(text) <= 0: raise ValueError


Hmm, I'm actually not so sure about this line now. It doesn't seem right
to raise a ValueError when the result of the expression is negative,
because even though it's a problem for my program, it isn't really a
"ValueError ," right?
It's obviously a ValueError if your program needs a strictly positive
integer. But anyway, you don't care: this error is raised just so it get
caught on the next line...
Aug 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1886
by: Francis Avila | last post by:
A few days ago (see the 'itertools.flatten()?' thread from October 28) I became obsessed with refactoring a recursive generator that yielded the leaves of nested iterables. When the dust settled, I had many flatten functions at hand. So I had to time them. Results below. History of the functions (from flattrial.py): # There are three basic features:
9
2292
by: wiredog | last post by:
I am struggling rewriting my query from MS Access' IIF, Then to SQL Servers TSQL language. I am hoping some one can give me some guidance. I believe I have the first portion of the query correct but do believe this requires a "NESTED" argument. This is where I am lost. My Original MS ACCESS Query reads-- SELECT DISTINCTROW REGION_TRAFIC.*, IIf(Mid(,5,2)=,
3
6472
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I COULD be wrong... :) I've tried the access group...twice...and all I get is "Access doesn't like ".", which I know, or that my query names are too long, as there's a limit to the length of the SQL statement(s). But this works when I don't try to...
2
5753
by: Asbjørn Ulsberg | last post by:
Hi. I'm trying to convert Brady Hegberg's great RTF2HTML VB 6.0 module to C#. I've managed to convert the VB code to VB.NET, which gave me the following code: Option Strict On Option Explicit On Option Compare Binary
4
2524
by: ECathell | last post by:
I had read an article at one time that suggested a pattern to get around deeply nested if..then..else hell... Can anyone point me in that direction? select case statements wont work for me in this instance -- --Eric Cathell, MCSA
2
2693
LonelyBunny
by: LonelyBunny | last post by:
The most unprobable issue will just pop-up when last expected..even as 'innocent' as a nested IF statement ! I am experiencing Nested IF statements problem chen coding. e.g: IF (meets a major condition) then if (another condition) then : : else : :
0
1087
by: Joeyeti | last post by:
Hi fellow VB knowers (I am but a learner still). I have a question for you which I struggle with. I need to convert nested Lists in MS WORD (whether numbered or bulleted or mixed) from their original format to a tag-formatted text (as used for instance for Wiki articles or phpBB Forums and such). In my particular case I need the text to have basic HTML tags for the basic formatting - e.g. <strong>text</strong> or <em>italics</em> - and this...
12
2583
by: desktop | last post by:
Are there any performance issues between: bool run = true; while(run) { if (bob1 &&){ //dothing break; }
13
2123
by: JRough | last post by:
I got lost on formatting the nested if/else's on the bottom of the file right up the last else/if there seems to be a stray bracket. I'm trying to line up the brackets so I can read it. Is it okay to split a long line setting a variable on two lines at the equals sign? That is so you can read it in google groups. <? //Send payement advices $TPL_auction_id = $auction_id; $user_id=$userrec;
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9484
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8983
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7505
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.