As Msf::Exploit::Remote::HttpClient.target_uri shows, using URI could raise an ::URI::InvalidURIError exception which should be handled.