Useful Chrome language feature: Conditional exception handlders
A conditional exception handler is an on e: … do block in an exception handler that has a where condition. This allows you to only catch an exception when all your conditions apply to it. Although we’ve had this feature since the first version of Chrome, it’s not a very well known.
/// Reads data from a socket. Returns 0 when the socket was disconnected or reset by peer class method Utilities.ReadData(Sock: Socket; Data: array of Byte): Integer; begin try Result := Sock.Receive(Data, 0, Data.Length, SocketFlags.None); except on e: SocketException where e.ErrorCode = 10054 do Result := 0; // Reset by peer end; end;
Above block reads data from a socket, but it catches the “Reset by peer” exception, and returns 0, like it’s a regular disconnect.