Python Forum
why is this try.....except statement not working?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why is this try.....except statement not working?
#1
Hi all,
I received an exception
UnicodeEncodeError: 'UCS-2' codec can't encode characters in position 34-34: Non-BMP character not supported in Tk
So I try to use the statement "try...except...else" to skip the exception, below is an example:
for repo_dict in repo_dicts:
    try:
        descr=repo_dict['description']
    except UnicodeEncodeError:
        descr='EncodeError'
    else:
        print('\nName:',repo_dict['name'])
        print('Owner:',repo_dict['owner']['login'])
        print('Stars:',repo_dict['stargazers_count'])
        print('Repository:',repo_dict['html_url'])
        print('Created:',repo_dict['created_at'])
        print('Updated:',repo_dict['updated_at'])
        print('Description:',descr)
The results I expected should be it will print out "EncodeError" if the repo_dict['descripton'] has UnicodeEncodeError, but it changes nothing...

So may I know where the issue locates?

Best Regards,
Henry
Reply
#2
don't know what else is for, try:
for repo_dict in repo_dicts:
    try:
        descr=repo_dict['description']
        print('\nName:',repo_dict['name'])
        print('Owner:',repo_dict['owner']['login'])
        print('Stars:',repo_dict['stargazers_count'])
        print('Repository:',repo_dict['html_url'])
        print('Created:',repo_dict['created_at'])
        print('Updated:',repo_dict['updated_at'])
        print('Description:',descr)
    except UnicodeEncodeError:
        Print('Unicode Encode Error')
This line can be removed:
descr=repo_dict['description']
Reply
#3
(Feb-06-2018, 02:48 AM)Larz60+ Wrote: don't know what else is for, try:
for repo_dict in repo_dicts: try: descr=repo_dict['description'] print('\nName:',repo_dict['name']) print('Owner:',repo_dict['owner']['login']) print('Stars:',repo_dict['stargazers_count']) print('Repository:',repo_dict['html_url']) print('Created:',repo_dict['created_at']) print('Updated:',repo_dict['updated_at']) print('Description:',descr) except UnicodeEncodeError: Print('Unicode Encode Error') 

Hi,
Thank you for your swift reply. You are right, I misunderstand the logic of else statement. I will try to think deeper next time before I post a thread here :-)
Reply
#4
No problem, you will never stop needing help once in a while.
I've been programming for many years, I need help all the time.
If you ever get to the point where you don't, that means you have stopped learning.

I expect I will stop when I'm lying in a box with a Lilly in my hands.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question If, elif, and else statement not working PickleScripts 3 902 Mar-30-2023, 02:53 PM
Last Post: PickleScripts
  If statement not working correctly? MrKnd94 2 843 Nov-16-2022, 02:49 AM
Last Post: deanhystad
  If Statement not working...Why? Milfredo 2 2,215 Oct-17-2020, 03:23 AM
Last Post: Milfredo
  Invoking function in if else statement, not working! ibaad1406 13 5,648 May-30-2019, 09:05 PM
Last Post: ibaad1406
  If statement not working oldcity 4 3,108 Oct-14-2018, 10:45 AM
Last Post: oldcity
  if statement not working trent101010 8 4,967 Mar-14-2018, 03:19 PM
Last Post: wavic

Forum Jump:

User Panel Messages

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