tags:

views:

32

answers:

1

Hi,

I am new to Android mobile application development. I would like to know , how can I handle the exception , like HttpConnection related exceptions or any other exceptions. Do I need to display an AlertDialog to the user.

Kindly provide a sample code or project source code on how can I handle HttpConnection or similar type of Exceptions.

Warm Regards,

CB

+1  A: 

How you handle exception depends on the exception. If the exception is something that you cannot recover from, and the user needs to know about then you could catch the exception and show it in an AlertDialog:

try {
  // do something
} catch (SomeImportantException e) {
  AlertDialog.Builder builder = new AlertDialog.Builder(this);
  builder.setMessage("User friendly text explaining what went wrong.")'
  AlertDialog alert = builder.create();
  alert.show();
}

For more info on the dialog, see creating dialogs.

Alternatively, if the exception is something that you can deal with, you can just log information about the exception and move on.

try {
  // do something
} catch (SomeLessImportantException e) {
  Log.d(tag, "Failed to do something: " + e.getMessage());
}
Mayra