Error: 4invalid UTF-8 sequence :: How can I catch this?? (or otherwise handle it)

Charles Hixson charleshixsn at earthlink.net
Wed Oct 21 22:21:14 PDT 2009


Daniel Keep wrote:
> Charles Hixson wrote:
>> I want to read a bunch of files, and if the aren't UTF, then I want to
>> list their names for conversion, or other processing.  How should this
>> be handled??
>>
>> try..catch..finally blocks just ignore this error.
> 
>> type stuff.d
> import std.stdio;
> import std.utf;
> 
> void main()
> {
>     try
>     {
>         writefln("A B \xfe C");
>     }
>     catch( UtfException e )
>     {
>         writefln("I caught a %s!", e);
>     }
> }
> 
>> dmd stuff && stuff
> A B I caught a 4invalid UTF-8 sequence!
> 
> Works for me.

Sorry, the error is on the read.  The code I tried to use was:

try	{	lin	=	fil.readLine;	}
catch
{  writefln("File <<" ~ filIter [curFilNdx] ~ ">> is not a valid UTF 
file.");
    fil.close;
    getLine;
   return;
}
finally
{	}
debug (9) writefln ("lin = <<" ~ lin ~ ">>");
try
{ validate (lin); }
catch	(UtfException ue)
{  writefln ("File <<" ~ filIter [curFilNdx] ~ ">> is not a valid UTF 
file.");
    fil.close;
    getLine;
    return;
}

where fil is a File and getLine is one of my routines that automatically 
switches to the next file if the current file has been closed.


More information about the Digitalmars-d-learn mailing list