Skip to content Skip to sidebar Skip to footer

Can't Catch Networkonmainthreadexception

I know what causes a NetworkOnMainThreadException, as well as how to fix it, but for the purposes of improving the development experience, I'd like to be able to catch the exceptio

Solution 1:

Is there something I don't know about Java (perhaps) that makes this exception uncatchable??

Yes, StrictMode makes it uncatchable. Either way though, you should not catch this exception. Instead, you should implement your code correctly by wrapping your code in an AsyncTask. The reason why this exception is thrown is to prevent you from slowing down your application by blocking the UI thread.

Read my blog post for more info:

Why Ice Cream Sandwich Crashes Your App

Solution 2:

Well I just tested this on my version of eclipse, and it works just fine.. I guess I would check which version of the api you are using? looks like to throw that exception you need a minimum api version 11. Otherwise perhaps eclipse is to blame? All I know is that this code is correct and should be executing without any issues.

Solution 3:

Are you sure this is the first use of networking in your application?

If you are connecting to a server usually at that time NetworkOnMainThreadException should be thrown. Try adding a log statement before the try and see if it shows up. If it does not the Exception is thrown earlier.

Post a Comment for "Can't Catch Networkonmainthreadexception"