3

I am total noob at Android dev. I am still learning, and I am on very first step of my app dev.

I have this code working and it runs fine or regular Java, now I am trying to implement to Android OS.

In my code where it says TEST.openStream() I get Unhandled exception type IOException error.

 package com.zv.android;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;

public class ZipActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            URL TEST = new URL("http://www.google.com/");
            BufferedReader in = new BufferedReader(new InputStreamReader(TEST.openStream()));

            String inputLine;
            int line=0;
            while ((inputLine = in.readLine()) != null){
                line++;
                       //System.out.println(line + "\t" + inputLine); 
            }
           // in.close();
        } catch(MalformedURLException e) {
            //Do something with the exception.
        }

    }
}

3 Answers 3

10

Error message is simple: you need to catch the IOException, this because URL.openStream() is declared as

public final InputStream openStream() throws IOException

so by accepting the contract of the method you also accept the fact that you must handle this exception, this is how it works in Java. This is a checked exceptions, then it must be caught because this kind of exceptions represent situations that may arise and that your code must handle.

To catch it just add another case in your try statement:

try {
  ..
catch (MalformedURLException e) {
  ..
}
catch (IOException e) {
  ..
}

Just as a final note: you don't need to catch it when you call the openStream() method, you could state that the method that calls openStream() will forward the exception to the caller but in the end of the call chain you will have to catch it in any case.

Sign up to request clarification or add additional context in comments.

Comments

3

Catch IOException also, like how you did for MalformedURLException (or) declare method as throws IOException.

try {
            URL TEST = new URL("http://www.google.com/");
            BufferedReader in = new BufferedReader(new InputStreamReader(TEST.openStream()));

            String inputLine;
            int line=0;
            while ((inputLine = in.readLine()) != null){
                line++;
                       //System.out.println(line + "\t" + inputLine); 
            }
           // in.close();
        } catch(MalformedURLException e) {
            //Do something with the exception.
        } catch(IOException e2) {
            //Do something with the exception.
        }

IOException is checked exception, which either need to be catch (or) re-throw. See this tutorial for more information.

You need to catch IOException because openStream() method may throw IOException in abnormal case.

1 Comment

Can you please explain me why we catch IOException as well ?
1
TEST.openStream()

might throw an IOException which is a checked exception in java, thus you have to either Handle the IOException using try/catch blocks or declare IOException in your method signature using throws clause.

you have to handle IOException in your catch block .

 try {
            URL TEST = new URL("http://www.google.com/");
            BufferedReader in = new BufferedReader(new InputStreamReader(TEST.openStream()));
             //rest of your code
             } catch(MalformedURLException e) {
            //Do something with the exception.
        }

        catch(MalformedURLException e) {
        //Do something with the exception.
         }
         catch(IOException ex) {
             ex.printStackTrace();
       }

Declaring IOException in your method signature:

 public void onCreate(Bundle savedInstanceState) throws IOException {

in this case you dont wrap your code inside a try/catch, although i would strongly recommend always handle the exception using try/catch rather than declaring it using throws clause.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.