Colour as bleeding obvious, but in HTMLUnit I could not figure out how to “just use WebClient.setTimeout(int)”.

I was searching for something like this:

try{
	webClient.setTimeout(60000);
	HtmlPage page = (HtmlPage) webClient.getPage("http://somepage");
}
catch(TimeoutError e) {
	//Try again, or abort, etc
}

Except TimeoutError is made-up. I was sure setTimeout() must cause an exception, otherwise what the hell was the point of it? But no amount of searching would give me the answer, I’d either end up on the trail to “just use WebClient.setTimeout(int)” or finding my exact question, but with no answer at all.

Turns out all I needed to do was to piece together these two bits of information:

  1. setTimeout Sets the timeout of the WebConnection.

  2. WebConnection - Throws: IOException - if an IO error occurs

Yes, handling timeouts is as simple as

try{
	webClient.setTimeout(60000);
	HtmlPage page = (HtmlPage) webClient.getPage("http://somepage");
}
catch(IOException e) {
	//This catches a timeout error as well
}

(Thankfully I figured this out before I asked on Stackoverflow - because it turns out there is such a thing as a stupid question)