1

I have a HttpHostConnectException... That is okay, because the server is offline. So I want to mange to catch this exception for the situation, the server will be down.

But if I use

catch (HttpHostConnectException e){
            e.printStackTrace();
}

Nothing happens and the exception will be kill the progess. So how can I catch "unreachable" servers? Thank your for your time and help ;)

Bolic
  • 705
  • 2
  • 12
  • 30

1 Answers1

2

Calling e.printStackTrace(); will Kill your app as the exception is not handled

e.printStackTrace(); will print the exception on to the logcat and Will show an error or will crash you app

Either you can display the exception as string or Make static text as toast saying server unreachable

catch (HttpHostConnectException e)

{

 Toast.makeText(getApplicationContext(), "Server Unreachable ", Toast.LENGTH_LONG).show();

}

if you want to show what was the actual problem / exception that was caused use

catch (HttpHostConnectException e)

{

 Toast.makeText(getApplicationContext(), "Connection Timeout Reason "+string.ValueOf(e), Toast.LENGTH_LONG).show();

}

By doing this your app will not kill itself and proceed to the next line of your code

you can also do this

Log.v(locat,  exception.toString());
Metalhead1247
  • 1,978
  • 1
  • 17
  • 28
  • what do you exactly mean with "exception is not handled"? How can I handle one? Now it is working, because I cancel the AsyncTask in which the http request is in when an Excption occures. But the printStackTrace() is still running. – Bolic Sep 18 '13 at 08:44
  • @Bolic the exception is printing to stacktrace and then the app will close .once your app gets an exception you are printing it to the stack and doing nothing with the exception – Metalhead1247 Sep 18 '13 at 09:45
  • thanks. It seems as I am not so tough in java as I thought. My opinion was, if I catch the exception it is well done. But I have to handle it if I caught one. – Bolic Sep 18 '13 at 11:52
  • @Bolic no problem :) if there is exception just let the user know by means of a toast so that he know there will be no result untill the server is back online – Metalhead1247 Sep 18 '13 at 12:00