4

What exception is called on internet connection lost while getting data from server ? I have an AsyncTask and I am getting in it JSON data from server. My question is : what exception is called when connection has been interrupted ? How can I implement that ?

TN888
  • 7,659
  • 9
  • 48
  • 84
  • Curious to know that why no just have a general `catch(Exception e){}` instead of defining a type? Have all the connectivity related statements inside the try block. – Shobhit Puri Aug 28 '13 at 20:41
  • I have implemented JSONException. – TN888 Aug 28 '13 at 20:43
  • 4
    @ShobhitPuri : Relying on only a 'catch all' for exceptions is not good programming practice and can actually be a lot less flexible. For example, if you catch specific exceptions it may be possible to recover and try again (depending on the scenario and what the operation is). – Squonk Aug 28 '13 at 20:51
  • I thing u can implement a Broadcast receiver. Check [this post][1] [1]: http://stackoverflow.com/questions/1783117/network-listener-android/1785300#1785300 – Arun Kumar K S Aug 28 '13 at 21:04
  • I thing u can do this u[][1]sing a Broadcast receiver. Check [this post][1] [1]: http://stackoverflow.com/questions/1783117/network-listener-android/1785300#1785300 – Arun Kumar K S Aug 28 '13 at 21:06
  • It would be `IOException` I believe. – Varun Aug 28 '13 at 21:12
  • @Squonk thanks for enlightening. :-) it makes sense. – Shobhit Puri Aug 29 '13 at 04:27

1 Answers1

1

General approach to this type of problem:

Write (temporarily) a catch all style code and write the type of exception somewhere. Test it by knowingly raising the given exception (in your case you can interrupt the connection to the internet). After you have seen the type of the exception you can return to your code and instead of the catch all style code handle the specific exception.

Cheers

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175