Android display my own error message ifrom an exception
<p>i have implemented a try block that does some network requests but whenever it throws an exception such has a Timeout one, the android application displays a message Saying</p>

<p>Sorry: The application has stopped unexpectedy, please try again.</p>

<p>the only option presented to the user is a force close button. i want to catch this exception and simple display my own error message and let the user try the connection again instead of the user having to close the app.</p>

<p>Here is the code below:</p>

<p>all i want to do is return null after a exception is caught and what ever class that calls this will do it its own error handling like this:</p>

<pre><code>if(postRequest == null){ display error message}

private HttpResponseObject PostRequest() {

        //int response = 0;

        String responseBody = "";
        HttpResponseObject response = new HttpResponseObject();

        try {

            Log.d(TAG, "IN POST_REQUEST_METHOD");
            Log.d(TAG, "URL : " + url);

            httpclient = new DefaultHttpClient();

            httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1);
            //httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
            httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5);
            Log.d(TAG, "httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);");
            post = new HttpPost(url);
//          ClientConnectionRequest connRequest = new ManagedClientConnection();
//          post.setConnectionRequest((ClientConnectionRequest) connRequest.getConnection(3000, TimeUnit.MILLISECONDS));

            ByteArrayEntity entity = new ByteArrayEntity(data.getBytes());
            entity.setChunked(true);

            post.setEntity(new StringEntity(data));

            HttpResponse httpResponse = httpclient.execute(post);

            HttpEntity resEntity = httpResponse.getEntity();


            if (resEntity != null) {
                // indicate that the content of this entity is no longer
                // required
                response.setResponseBody(EntityUtils.toString(resEntity));

                Header[] header = httpResponse.getAllHeaders();

                Log.d(TAG, "httpResponse.getAllHeaders() = " + header[0].getName());
                response.setResponseHeader(httpResponse.getAllHeaders().toString());
                resEntity.consumeContent();
            }

            // release all recources from the httpClient object
            httpclient.getConnectionManager().shutdown();

            response.setResponseCode(httpResponse.getStatusLine().getStatusCode() ) ;

            Log.d(TAG, "responseBody = " + response.getResponseBody());
            Log.d(TAG, "response code = " + response.getResponseCode());
            Log.d(TAG, "response header = " + response.getResponseHeader());

            return response;

        } catch (SocketTimeoutException e) {
            e.printStackTrace();
            Log.d(TAG, "SocketTimeoutException e  = " + e.toString());
            return null;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.d(TAG, "IOException e  = " + e.toString());
            return null;
        }

    }
</code></pre>
