c++ exception with a non-ascii message

T

toton

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
 
B

Bart

toton said:
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.
 
P

Pete Becker

toton said:
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.
 
B

benben

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
 
B

Bernd Strieder

Hello,
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
 
T

toton

Bart said:
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.


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.
 
P

Pete Becker

benben said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top