重新显示java中捕获的不同异常
我有以下加载功能。我在途中捕获了一些可能的异常,并将它们存储在一个异常类型变量中,在finally块中清理之后,我想重新抛出原始异常(如果捕获到一个),或者抛出我自己的自定义DownloadFailedException。问题是Eclipse给了我“未处理的异常类型异常”错误,因为我的函数没有声明抛出异常。有没有一个“好”的方法可以做到这一点重新显示java中捕获的不同异常,java,exception,throw,throws,rethrow,Java,Exception,Throw,Throws,Rethrow,我有以下加载功能。我在途中捕获了一些可能的异常,并将它们存储在一个异常类型变量中,在finally块中清理之后,我想重新抛出原始异常(如果捕获到一个),或者抛出我自己的自定义DownloadFailedException。问题是Eclipse给了我“未处理的异常类型异常”错误,因为我的函数没有声明抛出异常。有没有一个“好”的方法可以做到这一点 public static boolean downloadFile(String urlString, String dstPath) throws D
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;
}
如果您有:
// 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);
根据您描述的情况以及方法末尾的抛出的
子句,该代码执行您想要执行的操作。在英语中,该代码的作用如下:
- 如果捕获到一个
,并且它是一个异常
,那么您希望抛出一个IOException
IOException
- 如果捕获了一个
,但它不是异常
,则希望抛出一个IOException
(由您自己创建)。我们将任何非下载异常
包装在IOException
中,以便您的实现与方法上的downloadeexception
子句一致throws
- 如果你没有捕捉到一个
,那么你希望生活继续正常异常
请注意,在编译类之前,您可能需要向类
DownloadException
(如果不存在具有该签名的构造函数)中添加一个构造函数,如public DownloadException(Throwable e){super(e);}
。我会尝试,不过我需要添加一个“else if(IllegalStateException实例of IllegalException)”因为这是一个运行时异常(我认为它被称为unchecked,这就是为什么它不在函数的throws声明中),这是有意义的。对不起,我没从上面挑出来。除了测试IllegalArgumentException
,您还可以测试RuntimeException
和throw
任何匹配的内容(转换为RuntimeException
)。
if(exception instanceof IOException)
throw (IOException) exception;
else
throw new DownloadException(exception);