Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8795595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:25:20+00:00 2026-06-13T23:25:20+00:00

I’m trying to raise exception in python 2.7.x which includes a unicode in the

  • 0

I’m trying to raise exception in python 2.7.x which includes a unicode in the message. I can’t seem to make it work.

Is it not supported or not recommended to include unicode in error msg?
Or do i need to be looking at sys.stderr?

 # -*- coding: utf-8 -*-
 class MyException(Exception):
  def __init__(self, value):
    self.value = value
  def __str__(self):
    return self.value
  def __repr__(self):
    return self.value
  def __unicode__(self):
    return self.value

desc = u'something bad with field \u4443'

try:
  raise MyException(desc)
except MyException as e:
  print(u'Inside try block : ' + unicode(e))

# here is what i wish to make work 
raise MyException(desc)

Running script produces the output below.
Inside my try/except i can print the string without problem.

My problem is outside the try/except.

Inside try block : something bad with field 䑃
Traceback (most recent call last):
  File "C:\Python27\lib\bdb.py", line 387, in run
    exec cmd in globals, locals
  File "C:\Users\ghis3080\r.py", line 25, in <module>
    raise MyException(desc)
MyException: something bad with field \u4443

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:25:21+00:00Added an answer on June 13, 2026 at 11:25 pm

    The behaviour depends on Python version and the environment. On Python 3 the character encoding error handler for sys.stderr is always 'backslashreplace':

    from __future__ import unicode_literals, print_function
    import sys
    
    s = 'unicode "\u2323" smile'
    print(s)
    print(s, file=sys.stderr)
    try:
        raise RuntimeError(s)
    except Exception as e:
        print(e.args[0])
        print(e.args[0], file=sys.stderr)
        raise
    

    python3:

    $ PYTHONIOENCODING=ascii:ignore python3 raise_unicode.py
    unicode "" smile
    unicode "\u2323" smile
    unicode "" smile
    unicode "\u2323" smile
    Traceback (most recent call last):
      File "raise_unicode.py", line 8, in <module>
        raise RuntimeError(s)
    RuntimeError: unicode "\u2323" smile
    

    python2:

    $ PYTHONIOENCODING=ascii:ignore python2 raise_unicode.py
    unicode "" smile
    unicode "" smile
    unicode "" smile
    unicode "" smile
    Traceback (most recent call last):
      File "raise_unicode.py", line 8, in <module>
        raise RuntimeError(s)
    RuntimeError
    

    That is on my system the error message is eaten on python2.

    Note: on Windows you could try:

    T:\> set PYTHONIOENCODING=ascii:ignore
    T:\> python raise_unicode.py
    

    For comparison:

    $ python3 raise_unicode.py
    unicode "⌣" smile
    unicode "⌣" smile
    unicode "⌣" smile
    unicode "⌣" smile
    Traceback (most recent call last):
      File "raise_unicode.py", line 8, in <module>
        raise RuntimeError(s)
    RuntimeError: unicode "⌣" smile
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.