Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Try/Exept prints only ones
#5
Using a simplified version of your example:
cnttt = 1
cntt1 = 0
lstt0 = ['lst0']
lstt1 = []
try :
    print ("-- Count of 'Error - 0' Errors -- "+ str(cnttt)+" --->>>  "+lstt0[-1]+"\n")
    print ("-- Count of 'Error - 1' Errors -- "+ str(cntt1)+" --->>>  "+lstt1[-1]+"\n")                    
except Exception as ex:
    print(ex)
Output:
-- Count of 'Error - 0' Errors -- 1 --->>> lst0 list index out of range
This looks like the exception works. It prints the message followed by the exception. But what happens if we change lst0 = [] and lst1 = ['lst1']. When I do this the output is:
Output:
list index out of range
Why is there only 1 message? The answer is easy if we trace the code. We try to print the first message and it fails because the list is empty and there is nothing to get at lst0[-1]. This throws an exception and we move down to the handler that prints the exception. Execution continues on from that point. It does not jump back to the next line after the exception. Just imagine how horrible that would be if you wrapped an exception handler around a loop. You might never escape.

Wrapping an exception handler around each print statement fixes the problem, because each print statement gets evaluated. However I think this is a case where look before you leap is better than relying on an exception handler.
if (len(lstt0(> 0):
    print ("-- Count of 'Error - 0' Errors -- "+ str(len(lstt0))+" --->>>  "+lstt0[-1]+"\n")
if (len(lstt0) > 0):
    print ("-- Count of 'Error - 1' Errors -- "+ str(len(lstt1))+" --->>>  "+lstt1[-1]+"\n")                    
Reply


Messages In This Thread
Try/Exept prints only ones - by tester_V - Nov-01-2020, 04:13 AM
RE: Try/Exept prints only ones - by bowlofred - Nov-01-2020, 04:40 AM
RE: Try/Exept prints only ones - by tester_V - Nov-01-2020, 05:52 AM
RE: Try/Exept prints only ones - by tester_V - Nov-01-2020, 06:11 AM
RE: Try/Exept prints only ones - by deanhystad - Nov-01-2020, 10:14 PM
RE: Try/Exept prints only ones - by tester_V - Nov-02-2020, 01:58 AM
RE: Try/Exept prints only ones - by deanhystad - Nov-02-2020, 03:41 AM
RE: Try/Exept prints only ones - by tester_V - Nov-02-2020, 03:44 AM
RE: Try/Exept prints only ones - by tester_V - Nov-02-2020, 03:56 AM
RE: Try/Exept prints only ones - by deanhystad - Nov-02-2020, 04:00 AM
RE: Try/Exept prints only ones - by deanhystad - Nov-02-2020, 04:25 AM
RE: Try/Exept prints only ones - by tester_V - Nov-03-2020, 02:38 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  zfill prints extra et the end of a var tester_V 4 985 Mar-24-2023, 06:59 PM
Last Post: tester_V
  variable prints without being declared. ClockPillow 2 1,876 Jul-11-2021, 12:13 AM
Last Post: ClockPillow
  Output prints Account.id at the end? LastStopDEVS 5 2,885 Dec-19-2020, 05:59 AM
Last Post: buran
  loop only prints last character. mcmxl22 1 1,770 Feb-17-2020, 02:36 AM
Last Post: menator01
  can you understand why this code prints None? arcbal 2 2,817 Mar-13-2019, 02:57 AM
Last Post: arcbal
  What for a file that prints nothing sylas 1 2,229 Sep-12-2018, 01:18 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020