string need to be robust

Jesse Phillips jessekphillips+D at gmail.com
Sun Mar 13 23:02:44 PDT 2011


ZY Zhou Wrote:

> it doesn't make sense to add try/catch every time you use tolower/toupper/foreach
> on string. No one will do that.
> You either throw exception when convert invalid utf8 bytes to string, or never
> throw exception and use invalid UTF32 code in dchar to represent invalid utf8 code.
> 
>   string s = "\x0A"; // this is the right place to throw the exception (or compile
> error)

I don't think here is even a good place to have it. The problem being most strings/invalid strings will come in during run-time. And at run-time we have std.file.readText which will validate the string upon entry.

>   s.tolower; // no one will add try/catch on this

Right that isn't the point of throwing an exception. In this case you have not written you program to handle invalid Unicode, which means your program should not continue to operate until you decide to fix it. (Exceptions are robust)

However an access violation like the one received from using foreach is not appropriate and should be filed as a bug. You should be programming under the assumption that everything is how you expect it, and verify and remedy external inputs at the point of input. If you don't then the program shouldn't continue running.


More information about the Digitalmars-d mailing list