arhoskins
arhoskins

Reputation: 373

Android: NetworkOnMainThreadException not called

I'm familiar with Android development and used to the NetworkOnMainThreadException exception when doing a network call on the UIThread.

However in the following case the IOException is thrown and no NetworkOnMainThreadException is thrown.

Code:

        try {
            a = mGeo.getFromLocationName("SFO", 1);
        } catch (IOException e) {
            e.printStackTrace();
        }

I find this weird. Shouldn't the network exception (which is a runtime exception) be thrown?

Upvotes: 1

Views: 82

Answers (1)

RamIndani
RamIndani

Reputation: 698

If you check implementation on link then IOException is the checked exception and is thrown from the code. for reference I am adding implementation from above link more can be found in comments in implementation

public List<Address> getFromLocationName(String locationName, int maxResults) throws IOException {
if (locationName == null) {
    throw new IllegalArgumentException("locationName == null");
}
try {
    List<Address> results = new ArrayList<Address>();
    String ex = mService.getFromLocationName(locationName,
        0, 0, 0, 0, maxResults, mParams, results);
    if (ex != null) {
        throw new IOException(ex);
    } else {
        return results;
    }
} catch (RemoteException e) {
    Log.e(TAG, "getFromLocationName: got RemoteException", e);
    return null;
}
}

NetworkOnMainThreadException is an unchecked exception as it extends RuntimeException, this link from java documentation may help. Hope this answers your question.

Upvotes: 4

Related Questions