Handling Error - "Low Surrogate char"

P

Phillip

I just ran into this problem also. (undocumented exception thrown when
Streamwriter is handed a buffer with wierd characters in it).

I was also able to get around it by specifying utf8 as the encoding
for Streamwriter. (Thank you Amy L!)

But I have some questions:
1- Isn't utf8 supposed to be the default mode for streamwriter? If so
why does specifying the default fix the problem?

2- I had opend up the file for appendding and was executing a
"WriteLine(String);" command when I got that exception. The problem is
the streamwriter handle is now 'broken'. You can't write anything
else to the file, you can't close it. The only way around it this
problem is to exit the program.
This seems like a bug in the streamwriter, it should have at the very
least closed the handle so I wouldn't have to exit the program.

Thanks
Phil Ouellette.
 
J

Jon Skeet [C# MVP]

Phillip said:
I just ran into this problem also. (undocumented exception thrown when
Streamwriter is handed a buffer with wierd characters in it).

I was also able to get around it by specifying utf8 as the encoding
for Streamwriter. (Thank you Amy L!)

But I have some questions:
1- Isn't utf8 supposed to be the default mode for streamwriter? If so
why does specifying the default fix the problem?

When you construct a UTF8Encoding object, you can specify whether or
not it should throw an exception when an invalid encoding is detected.
I suspect that the one used by default doesn't have the same setting
for that as the one you used (however you constructed it, or whether
you used Encoding.UTF8).
2- I had opend up the file for appendding and was executing a
"WriteLine(String);" command when I got that exception. The problem is
the streamwriter handle is now 'broken'. You can't write anything
else to the file, you can't close it. The only way around it this
problem is to exit the program.
This seems like a bug in the streamwriter, it should have at the very
least closed the handle so I wouldn't have to exit the program.

That certainly sounds odd. Could you post a short but complete program
which demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

If I can confirm it, I'll try to make sure it gets fixed.
 

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

Top