Muzaffar Mohamed
Muzaffar Mohamed

Reputation: 73

Android unreported exception IOException error

I am new to android apps development. Recently,Im writing an application which able to show public ip based on Ipify. So far, i already:

How to use Ipify, according to its website:

import org.ipify.Ipify;

public class HelloIP {
    public static void main(String[] args) throws IOException {
        System.out.println(Ipify.getPublicIp());
    }
}

I write the following method to be invoked from another class:

public static String getPublicIp() throws IOException{
    String ip = Ipify.getPublicIp();
    return ip;
}

Another Class

//Get wifi
getWifiName wifiInfo = new getWifiName();
String myIP = wifiInfo.getPublicIp();

However, i keep getting:

Error:(52, 43) error: unreported exception IOException; must be caught or declared to be thrown

I tried to modify the code and use the following try and catch, but still got the same error.

public static String getPublicIp() throws IOException{
    String myip = Ipify.getPublicIp();
    try{
        return myip;
    }
    catch (IOException e){
        System.out.println("General I/O exception: " + e.getMessage());
        e.printStackTrace();
    }
}

Im not too good in catch and throw exception, and already spent the whole day for this.I dont have idea anymore to fix this error..T.T

Upvotes: 1

Views: 362

Answers (1)

xenteros
xenteros

Reputation: 15842

public static String getPublicIp() {
    try{
        return Ipify.getPublicIp();
    }catch (IOException e){
        System.out.println("General I/O exception: " + e.getMessage());
        e.printStackTrace();
    }
    return null;
}

In case it didn't help, clean project in your IDE. You may have some data cached and it might be a reason.

Your problem is in another class! As you have declared the method getPublicIp() to throw IOException that class is afraid of receiving the Exception and therefor requests catching it.

In Java you have two types of Exceptions. Checked and unchecked. Checked Exceptions must be caught.

In Java Exceptions are used for marking unexpected situations. For example parsing non-numeric String to a number (NumberFormatException) or calling a method on a null reference (NullPointerException). You can catch them in many ways.

Unchecked Exceptions are those which extend RunTimeException. They are used for marking unexpected states usually caused by user's input. They shouldn't cause harm and should be worked out with business logic. You don't have to catch them, but sometimes you should.

On the other hand there are Checked Exceptions which mark dangerous situations. For example the application being unable to open a file. As those situations are found dangerous, you must catch them.

try{
    //some code
} catch (NumberFormatException e1) {
    e.printStackTrace()     //very important - handles the Exception but prints the information!
} catch (NullPointerException e2) {
    e.printStackTrace();
}

or using the fact, that they all extend Exception:

try {
    //somecode
} catch (Exception e) {
    e.printStackTrace;
};

or since Java 7:

try {
    //somecode
} catch (NullPointerException | NumberFormatException e) {
    e.printStackTrace;
};

Upvotes: 1

Related Questions