browse: forums | FAQ
Connecting Tech Pros Worldwide

Hey there! Do you need .NET Framework help?

Get answers from our community of .NET Framework experts on BYTES! It's free.

How to catch (or prevent) "Found a low surrogate char..." error

=?Utf-8?B?RGF2aWQ=?=
Guest
 
Posts: n/a
#1: Mar 28 '07
I need to convert an incoming byte array to a Unicode string, when (if) that
byte array contains valid Unicode values (sometimes it doesn't).

I've been trying to use UnicodeEncoding.GetString(bytes)...

However, when the byte array doesn't contain value Unicode values, I get a
"Found a low surrogate char without a preceding high surrogate at index..."
exception.

I understand why the error occurs, but I don't understand how to catch (and
ignore) the exception. For example, fhe following doesn't work...

Try
Dim c As String = enc.GetString(bytes)
Catch ex As Exception
...
End Try

.... the Catch is never entered.

So, how do I...

1) catch the exception

OR (preferably)

2) do a "test" on the byte array to ensure it contains valid Unicode values
BEFORE I actually do the conversion (which to me would seem to be more
efficient than causing/catching/ignoring an exception).

BTW: this is .NET 1.1

????






Closed Thread