It is unexpected behavior for ToString to raise an exception. It is expected to do a best-effort attempt at formatting but never fail.
Catch the exception and simply print unknown inv types as hexadecimal.
Before:
************************
EXCEPTION: St12out_of_range
CInv::GetCommand(): type=666 unknown type
bitcoin in ProcessMessages()
After:
2016-11-09 10:08:20 got inv: 0x0000029a 0000000000000000000000000000000000000000000000000000000000000000 have peer=1
Fixes #9110.