Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Try/Exept prints only ones
#11
I am now more confused.

You had one problem for sure. If you have a statement that might raise an exception, and you want to guarantee you will execute a second statement, the exception wrapper cannot enclose both statements. In the following code it is really important that I execute both print statements
items1 = []
items2 = [1]
try:
    print(items1[-1])
    print(items2[-1])
except Exception as ex:
    print(ex)
As you are aware, the first print statement throws an exception because items1 is empty and -1 is not a valid index for an empty list.

I can make sure both print commands executes if I move the second print statement outside the exception block.
items1 = []
items2 = [1]
try:
    print(items1[-1])
except Exception as ex:
    print(ex)
print(items2[-1])
This will print items2, but it does not protect against the possibility that items2 is empty. To protect against that you wrap the print inside another exception.
items1 = []
items2 = [1]
try:
    print(items1[-1])
except Exception as ex:
    print(ex)
try:
    print(items2[-1])
except Exception as ex:
    print(ex)
So now I get an error for the first print and the second print is successful.

If I had 10 different items lists this would result in ugly code if I continued to treat each list as variable. When working with more than 2 similar things I start thinking about using collections.
items = [[1], [2], [3, 4], [], [5, 6, 7]]
for item in items:
    try:
        print(item[-1])
    except Exception as ex:
        print(ex)
Output:
1 2 4 list index out of range 7
Even though I only have 1 exception block it protects the printing of all 5 lists. If you have more than two error types you could do something similar. I think I would use a dictionary.
import random

errors = {'Error 1':[], 'Error 2':[], 'Error 3':[]}

for a in range(1, 100):
    b = f'Error {random.randint(1, 10)}'
    for err_type in errors:
        if err_type in b:
            errors[err_type].append(a)

for err_type, lineno in errors.items():
    try:
        print(f'{err_type} count = {len(lineno)}, Last = {lineno[-1]}')
    except:
        pass
Output:
Error 1 count = 26, Last = 98 Error 2 count = 9, Last = 92 Error 3 count = 7, Last = 91
I hope one of these posts has answered your question. We have discussed the fact that an exception jumps you to the exception handler and that other code in the exception block may not get executed.

We discussed that if you have code with multiple statements that can throw an exception you may have to provide an exception handler for each statement. I also gave two examples for how this can be done serially with one exception block in a loop.

We have also discussed how sometimes it is better to wrap code inside an if statement than to use an exception handler.

And I have answered the question about why your program only prints out 1 or 2 lines per log file.

I really hope one of those answers your question.
tester_V likes this post
Reply
#12
To deanhystad. You are DA Man bro! Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What for a file that prints nothing sylas 1 1,085 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