-1

In my Android Studio project, I found lots of "Error:(38, 45) error: unreported exception IOException; must be caught or declared to be thrown" errors. How do I resolve these errors?

This is the code I have:

public class ActivityHome extends Activity {

    private final static String QUEUE_NAME = "AndroidJava";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("xx.xx.xx.xx");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        String message = "Hello Java!";
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
        System.out.println(" [x] Sent '" + message + "'");

        channel.close();
        connection.close();

    }

}
Brandon McKenzie
  • 1,655
  • 11
  • 26
QuickLearner
  • 89
  • 6
  • 13

1 Answers1

1

I would suggest reading though the Javadocs on the dependencies used in ActivityHome.onCreate() to determine what throws an IOException, especially your database related javadocs. Once you have determined what throws your exceptions, you can use a try catch block as follows to handle the possible issues:

try{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("xx.xx.xx.xx");

    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    String message = "Hello Java!";
    channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
    System.out.println(" [x] Sent '" + message + "'");

    channel.close();
    connection.close();
} catch(IOException e) {
    e.printStackTrace() //Or log it to a file, or clean up resources
}

or throws to force any method that calls yours to handle it instead:

public onCreate(Bundle savedInstanceState) throws IOException { 
    //body
}

The Java Guide to Exceptions can help with determining which is more appropriate for your use case.

Community
  • 1
  • 1
Brandon McKenzie
  • 1,655
  • 11
  • 26
  • Thanks Brandon. I do not know why the IOException just occurs on Android studio rather than Eclipse. Actually, the program can work well on Eclipse. When I add try catch on android studio like try{} ... catch(Exception e){ statement1; } The results will be statement1. – QuickLearner Jun 02 '16 at 17:14
  • The primary reason is because your Eclipse code had `throws Exception` in the method that calls those. You know how I mentioned that `throws` forces the caller to handle the exception? The methods you have with that error are in error because they use `throws IOException` and the compiler is doing its job of forcing you to handle the exception. In Eclipse, you handled it by throwing the Exception to the caller, but did not do so in Android Studio. – Brandon McKenzie Jun 02 '16 at 17:21
  • When I use try{ //offending code} catch(IOException e) { e.printStackTrace() //Or log it to a file, or clean up resource}. The errors still exist. – QuickLearner Jun 02 '16 at 17:25
  • What did you put inside the try block? Did it encapsulate everything that throws an `IOException`? I edited my answer to demonstrate. – Brandon McKenzie Jun 02 '16 at 17:26
  • Thanks. By using the codes you provided, I found the new error is: Error:(39, 23) error: unreported exception TimeoutException; must be caught or declared to be thrown. – QuickLearner Jun 02 '16 at 17:40
  • You can add that to your existing catch: `catch(IOException | TimeoutException e)`, catch a more general class: `catch(Exception e)`, or add another catch block `catch(IOException e) { } catch(TimeoutException e2) { }`, depending on how you wish to respond to these exceptions in your code. – Brandon McKenzie Jun 02 '16 at 17:43
  • Great. The errors have been deleted. I add "Toast.makeText(getApplicationContext(), "Exception", Toast.LENGTH_LONG).show();" in catch(IOException | TimeoutException e) {}. Then, when I run the codes, the "Exception" toast exists. The program do not connect Host as what it did on eclipse. I am confused of this result. – QuickLearner Jun 02 '16 at 18:05
  • Since that's out of scope for this question, I would ask it as a new one. – Brandon McKenzie Jun 02 '16 at 18:14