473,804 Members | 3,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c++ exception with a non-ascii message

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

Oct 27 '06 #1
6 1845
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.

Oct 27 '06 #2
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.
Oct 27 '06 #3
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
Oct 27 '06 #4
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

Oct 27 '06 #5

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.
Oct 27 '06 #6
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.
Oct 27 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
2399
by: cody | last post by:
public DateTime Value { get { try { return new DateTime(int.Parse(tbYear.Text), int.Parse(tbMonth.Text), int.Parse(tbDay.Text)); } catch (FormatException)
1
4232
by: Old Wolf | last post by:
1. What is the difference between #include <stdexcept> and #include <exception> ? 2. Is there a list somewhere of what each standard exception is used for? either to throw them, or throw user-defined exceptions derived from them? (for example I have been deriving mine from std::bad_alloc if there was a memory problem, or std::bad_exception if there was some other problem) 3. Is it a good idea to make all user-defined exceptions derive...
11
3279
by: Master of C++ | last post by:
Hi, I am writing a simulation package in C++, and so far I've written about 8000 lines of code and have about 30 classes. I haven't used C++ exceptions so far (for various reasons). The only two "resources" I use are memory and file I/O and whenever there is a memory allocation failure or file I/O failure I just simply call a custom assert-type function to check, print a error message and abort. This seems to be OK for now (for the...
44
4235
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box, etc.). 2. Low-level operations that are used to carry out the high level tasks
7
3160
by: cs | last post by:
We have some code that walks the process tree on windows xp. It uses the process class in system diagnostic as well as some dllimport calls to kernel32.dll and ntdll.dll We also have some code that talks on terminal services virtual channels using wtsapi32.dll What we have noticed is that after calling those methods several times per minute in some cases or at least lots of times per hour all day long will end up giving a "application...
7
6984
by: Mr Flibble | last post by:
Are try { //something } catch { // }
7
5149
by: Sek | last post by:
Hi Folks! I was pondering over a code and noticed that exception handlers were present in the private, protected as well as public methods. And, ofcourse, public methods were calling priv/prot methods internally. My thought was, the exception is being rethrown and propagated by the non-public methods to the public methods, causing performance overhead (stack winding etc). I do agree that, the purpose of throwing the exception by...
17
3406
by: cashdeskmac | last post by:
Nice and simple one for you all... Is there a time to use Catch(Exception) rather than creating an instance of the Exception, as in Catch(Exception ex)?
7
2015
by: j.l.olsson | last post by:
Hello, I am using std::auto_ptr to try to keep ownership of resources straight. However, I became aware that exception objects must provide a copy constructor, which made me uncertain of the soundness of throwing std::auto_ptrs... It was explained to me that a possible problem case is: try {
10
1771
by: Pranav | last post by:
#include "stdafx.h" #include<conio.h> #include<stdio.h> #include<string.h> int main() { char *str1 = "United"; char *str2 = "Front";
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10578
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.