Python Forum

Full Version: why is this try.....except statement not working?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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']
(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 :-)
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.