1

次のダウンロード機能があります。途中でいくつかの可能性のある例外をキャッチし、それらを Exception 型変数に格納します。finally ブロックでクリーンアップした後、元の例外を再スローするか (キャッチされた場合)、独自のカスタム DownloadFailedException をスローします。 . 問題は、関数がスロー例外を宣言していないため、Eclipse が「ハンドルされていない例外タイプの例外」エラーを表示することです。これを行う「いい」方法はありますか?

public static boolean downloadFile(String urlString, String dstPath) throws DownloadFailedException, IOException {
    if (!Settings.isNetworkAvailable()) {
        throw new NoNetworkException("Network error: no internet connection. Failed downloading " + urlString);
    }
    InputStream input = null;
    BufferedOutputStream output = null;
    int fileLength = -1;
    long total = 0;
    int statusCode = -1;
    HttpGet get = new HttpGet(urlString);
    get.setHeader("User-Agent", Settings.getUserAgent());
    get.setHeader("X-My-Id", Settings.getDeviceId());
    HttpClient client = new DefaultHttpClient();
    HttpResponse response = null;
    try {
        response = client.execute(get);
        statusCode = response.getStatusLine().getStatusCode();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (200 != statusCode) {
            throw new DownloadFailedException("http error: " + statusCode +". Failed downloading " + urlString, statusCode);
        }
    }
    if (null != response) {
        HttpEntity entity = response.getEntity();
        File tmpFile = null;
        Exception exception = null;
        try {
            InputStream is = entity.getContent();
            byte b[] = new byte[1];
            is.read(b, 0, 0);
            fileLength = (int)entity.getContentLength();
            input = new BufferedInputStream(is, 8192);
            tmpFile = new File(dstPath + ".tmp");
            tmpFile.createNewFile();
            output = new BufferedOutputStream(new FileOutputStream(tmpFile), 8192);

            byte data[] = new byte[8192];
            int count;
            while ((count = input.read(data)) != -1) {
                total += count;
                output.write(data, 0, count);
            }
        } catch (IllegalStateException e) {
            exception = e;
            e.printStackTrace();
        } catch (IOException e) {
            exception = e;
            e.printStackTrace();
        } finally {
            try {
                if (null != output) {
                    output.flush();
                    output.close();
                }
                if (null != input)
                    input.close();
            } catch (IOException e) {
                if (null == exception)
                    exception = e;
            }
            if (-1 < fileLength && total != fileLength) {
                if (null != tmpFile) {
                    tmpFile.delete();
                }
                if (null != exception) {
// HERE I WOULD LIKE TO RE-THROW THE ORIGINAl EXCEPTION
                    throw exception; // Unhandled exception type Exception
                    //also tried: exception.getClass().cast(exception);
                } else
                    throw new DownloadFailedException(urlString + ": only " + total + " bytes read out of " + fileLength);
            }
            File dstFile = new File(dstPath);
            tmpFile.renameTo(dstFile);
        }
        return true;
    }
    return false;
}

解決:

if (null != exception) {
    if (exception instanceof IllegalStateException)
        throw (IllegalStateException) exception;
    else if (exception instanceof IOException)
        throw (IOException) exception;
    else
        throw new RuntimeException(message, exception);
}
//throw new IOException("Only " + total + "bytes read from " + fileLength);
throw new DownloadFailedException(message);
4

1 に答える 1

3

あなたが持っている場所:

// HERE I WOULD LIKE TO RE-THROW THE ORIGINAl EXCEPTION
throw exception; // Unhandled exception type Exception
// also tried: exception.getClass().cast(exception);

私はこれを使用します:

if(exception instanceof IOException)
    throw (IOException) exception;
else
    throw new DownloadException(exception);

説明した状況とthrowsメソッドの最後の句に基づいて、そのコードはやりたいことを実行します。英語では、そのコードは次のようになります。

  • をキャッチしException、それが のIOException場合は、 をスローします。IOException
  • をキャッチしException、それが でない場合は、 (自分で作成IOExceptionした) をスローします。実装がメソッドの句と一致するように、非を a でDownloadExceptionラップします。IOExceptionDownloadExceptionthrows
  • をキャッチしない場合はException、通常どおりに生活を続けたいと考えています

public DownloadException(Throwable e){super(e);}これをコンパイルする前に、クラスにコンストラクターを追加する必要がある場合があることに注意してくださいDownloadException(そのシグネチャを持つコンストラクターがまだ存在しない場合)。

于 2013-04-21T19:28:07.070 に答える