try { //enter statements that can cause exceptions }
URL url = new URL('http://exampleurl.com/'); HttpURLConnection client = (HttpURLConnection) url.openConnection();
URL url = new URL('http://exampleurl.com/'); HttpURLConnection client = null; try { client = (HttpURLConnection) url.openConnection(); }
client.setRequestMethod('POST'); client.setRequestProperty('Key','Value'); client.setDoOutput(true);
OutputStream outputPost = new BufferedOutputStream(client.getOutputStream()); writeStream(outputPost); outputPost.flush(); outputPost.close();
client.setFixedLengthStreamingMode(outputPost.getBytes().length); client.setChunkedStreamingMode(0);
catch(MalformedURLException error) { //Handles an incorrectly entered URL } catch(SocketTimeoutException error) { //Handles URL access timeout. } catch (IOException error) { //Handles input and output errors }
finally { if(client != null) // Make sure the connection is not null. client.disconnect(); }