I'm not sure if my code is safe upon an error inside constructor what will happen? Should I change the way I wrote the code or is just fine?
What happen to a new obj that constructor throw null exception or fail to executed?
private Response _response = Response.NONE;
private long _time = System.currentTimeMillis();
public Request(final Site site)
{
final Holder holder = Holder.getInstance().getHolder(site);
if (holder == null)
{
throw new NullPointerException("Failed to find holder");
}
HttpURLConnection connection = null;
try
{
final URL url = new URL(site.toString());
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent", "");
connection.connect();
final String streamResponse = new BufferedReader(new InputStreamReader(connection.getInputStream())).lines().collect(Collectors.joining("\n"));
switch (site)
{
case X:
_response = Response.SUCCESS;
_time = <some code here>
break;
}
}
catch (final IOException e)
{
}
finally
{
if (connection != null)
{
connection.disconnect();
}
}
}
public Response getResponse()
{
return _response;
}
public long getTime()
{
return _time;
}