P.Henderson
P.Henderson

Reputation: 1091

Java/Android UnknownHostException

I have the following code to pass an IP to some package that looks up the country.

WebServiceClient client = new WebServiceClient.Builder(42, "license_key").build();
InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
CountryResponse response = client.country(ipAddress);

The problem is, that this gives me the error:

unreported exception UnknownHostException; must be caught or declared to be thrown

What am I doing wrong?

Upvotes: 0

Views: 99

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93542

One of those functions (at least) declares that exception as throwable. In Java, all declared throwable exceptions must be caught- you don't need to do anything in the exception handler (although logging it at least is a good idea), but it has to be there, or the function that calls it must also declare those exceptions as throwable (and its caller would then need to catch them). Its a mechanism in Java called "checked exceptions". A lot of people don't like them and many consider it to be a mistake, but those parts of the Java library that use it will probably never change, so you have to live by the rules.

Upvotes: 1

Related Questions