如何避免捕获IOException?

时间:2013-11-07 13:45:32

标签: java android android-asynctask

我有以下doInBackground方法的AsyncTask。这应该是读取和写入流。问题是连接断开连接在buffer为空之前调用IOException(它在while之前离开len != -1)并且我想避免这种情况。

protected Boolean doInBackground(String... StringUrls) {

    try {

   // ...

 len = in.read(buffer);

        while (len != -1) {

            bufOutstream.write(buffer, 0, len);
            len = in.read(buffer);

            if (Recorder.this.isCancelled)  {

            Recorder.this.stopSelf();
                break;
            }

        }


        bufOutstream.close();

    } catch (IOException e) {
        System.err.println("Caught IOException: " + e.getMessage());

        // 
    }


   return true;
}

3 个答案:

答案 0 :(得分:0)

抛出异常时,len保存上一次读取迭代的值,即上一次超过它。

无论如何,我建议:

    try
    {
        while ( (len = in.read()) > -1 )
        {

            bufOutstream.write(buffer, 0, len);

            if (Recorder.this.isCancelled)  
            {

                Recorder.this.stopSelf();
                break;
            }
        }
    }
    catch ( Exception e )
    {
        System.err.println("Caught IOException: " + e.getMessage());
    }
    finally
    {
        if ( in != null )
        {
            try
            {
                in.close();
            }
            catch ( Exception e )
            {
                // And I don't caaare
            }
        }

        if ( bufOutstream != null )
        {
            try
            {
                bufOutstream.close()
            }
            catch ( Exception e )
            {
                // And I don't caaare
            }
        }
    }

答案 1 :(得分:0)

如果您不想退出while循环,则需要在while循环中移动try catch块。不过,我建议退出循环。这是我将使用的代码:

protected Boolean doInBackground(String... StringUrls) {
    Boolean success = true;
    len = 0;

    try {
        while (len != -1) {
            bufOutstream.write(buffer, 0, len);
            len = in.read(buffer);

            if (Recorder.this.isCancelled)  {

            Recorder.this.stopSelf();
                break;
            }
        }
    } catch (IOException e) {
            // do something with the exception and if you can't handle it here 
            // rethrow it!
            success = false;
    } finally {
        try {
            bufOutstream.close();
        } catch (Exception e) {
            // do something with the exception and if you can't handle it here 
            // rethrow it!
        }
    }

    return success;
}

答案 2 :(得分:0)

在这种情况下,您可以检查while循环中的条件:
if您发现即使len != -1没有出现错误,也会抛出异常 然后吞下IOException,else你可以专门抛出它。

protected Boolean doInBackground(String... StringUrls) {

        try {
            //...
            int  len = in.read(buffer);

            while (len != -1) {
                try{
                bufOutstream.write(buffer, 0, len);
                len = in.read(buffer);

                if (Recorder.this.isCancelled)  {
                Recorder.this.stopSelf();
                    break;
                }
                }catch(IOException ioe){
                    if(len == -1){
                        throw new IOException(ioe);
                    }
                        /*IOException gets swallowed in case len != -1 is not met*/
                }
            }
            bufOutstream.close();

        } catch (IOException e) {
            System.err.println("Caught IOException: " + e.getMessage());
            // ...
        }
    }