c++ exception with a non-ascii message

Discussion in 'C++' started by toton, Oct 27, 2006.

  1. toton

    toton Guest

    Hi,
    I am c++ standard exceptions like out_of_range, but want it to have
    some nonascii message. However what returns a char* and the class is
    not templated.
    Do I need to write my own exception class for this purpose?
    Same problem I am facing with fstream. The open function is
    non-templated and takes a char* as file name. The file is always ascii,
    thus no wstream is needed, but the file-name sometime contains
    non-ascii character.

    Thanks
     
    toton, Oct 27, 2006
    #1
    1. Advertising

  2. toton

    Bart Guest

    toton wrote:
    > Hi,
    > I am c++ standard exceptions like out_of_range, but want it to have
    > some nonascii message. However what returns a char* and the class is
    > not templated.
    > Do I need to write my own exception class for this purpose?


    Yes.

    It's probably a good idea to write your own exception hierarchy anyway.
    The standard exceptions are way to broad to be useful in real programs.

    > Same problem I am facing with fstream. The open function is
    > non-templated and takes a char* as file name. The file is always ascii,
    > thus no wstream is needed, but the file-name sometime contains
    > non-ascii character.


    This one comes up pretty often. I think it should be a FAQ. Basically,
    you have to convert from wide to narrow characters yourself. Some
    compilers have a templated constructor as an extension, but the
    standard C++ version doesn't. I'm not sure whether the standards
    comittee intends to fix this or not.

    Regards,
    Bart.
     
    Bart, Oct 27, 2006
    #2
    1. Advertising

  3. toton

    Pete Becker Guest

    toton wrote:
    > Same problem I am facing with fstream. The open function is
    > non-templated and takes a char* as file name. The file is always ascii,
    > thus no wstream is needed, but the file-name sometime contains
    > non-ascii character.


    In C++0x, file streams will have overloaded constructors and open()
    member functions. One overload will take a const char* and the other
    will take a const string&.

    --

    -- Pete

    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." For more information about this book, see
    www.petebecker.com/tr1book.
     
    Pete Becker, Oct 27, 2006
    #3
  4. toton

    benben Guest


    > In C++0x, file streams will have overloaded constructors and open()
    > member functions. One overload will take a const char* and the other
    > will take a const string&.
    >


    I don't see that move solving non-ASCII character file name problem,
    however.

    Ben
     
    benben, Oct 27, 2006
    #4
  5. Hello,

    toton wrote:
    > I am c++ standard exceptions like out_of_range, but want it to have
    > some nonascii message. However what returns a char* and the class is
    > not templated.


    I think UTF-8 has been designed to serve this purpose, as extension of
    ASCII which needs only 8-bit chars to work. You should not need to do
    anything special, if UTF-8 is supported on your platform. The same
    holds for filenames.

    Bernd Strieder
     
    Bernd Strieder, Oct 27, 2006
    #5
  6. toton

    toton Guest

    Bart wrote:
    > toton wrote:
    > > Hi,
    > > I am c++ standard exceptions like out_of_range, but want it to have
    > > some nonascii message. However what returns a char* and the class is
    > > not templated.
    > > Do I need to write my own exception class for this purpose?

    >
    > Yes.

    Thanks. I will use my own exception hierarchy, not inheriting from
    std::exception.
    > It's probably a good idea to write your own exception hierarchy anyway.
    > The standard exceptions are way to broad to be useful in real programs.
    >
    > > Same problem I am facing with fstream. The open function is
    > > non-templated and takes a char* as file name. The file is always ascii,
    > > thus no wstream is needed, but the file-name sometime contains
    > > non-ascii character.

    >
    > This one comes up pretty often. I think it should be a FAQ. Basically,
    > you have to convert from wide to narrow characters yourself. Some
    > compilers have a templated constructor as an extension, but the
    > standard C++ version doesn't. I'm not sure whether the standards
    > comittee intends to fix this or not.

    At present I am converting it using current locale. But looks it is not
    always possible to have such conversion.
    > Regards,
    > Bart.
     
    toton, Oct 27, 2006
    #6
  7. toton

    Pete Becker Guest

    benben wrote:
    >
    >> In C++0x, file streams will have overloaded constructors and open()
    >> member functions. One overload will take a const char* and the other
    >> will take a const string&.
    >>

    >
    > I don't see that move solving non-ASCII character file name problem,
    > however.
    >


    I don't know what the "non-ASCII character file name problem is." If
    your OS uses a character encoding other than ASCII, presumably your
    compiler supports it, as well. That's not a standards issue.

    --

    -- Pete

    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." For more information about this book, see
    www.petebecker.com/tr1book.
     
    Pete Becker, Oct 27, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TOXiC
    Replies:
    5
    Views:
    1,283
    TOXiC
    Jan 31, 2007
  2. Jochen Lehmeier

    DBD::Oracle, Unicode, non-UTF8-non-ASCII strings

    Jochen Lehmeier, Jul 23, 2009, in forum: Perl Misc
    Replies:
    0
    Views:
    425
    Jochen Lehmeier
    Jul 23, 2009
  3. W. Trevor King
    Replies:
    0
    Views:
    132
    W. Trevor King
    Jan 24, 2013
  4. bruce
    Replies:
    38
    Views:
    293
    Mark Lawrence
    Nov 1, 2013
  5. MRAB
    Replies:
    0
    Views:
    102
Loading...

Share This Page