1
在我的应用程序中,我有一个工具来下载参考数据更新。用户可以在PreferenceActivity
中修改基本网址 - 然后将实际文件名追加到基本网址。当我尝试下载文件时,如果出现错误,可能会抛出异常。我想向用户展示最合适的错误消息,而不是简单地“发生错误”。要做到这一点,我想要捕捉个别的异常和相应的格式信息。那么,下载文件时会抛出什么异常呢?对于参考,这是我下载的代码(简化):下载文件时可能会抛出什么异常?
int msgId;
try {
String url = props.getProperty(Constants.SETTINGS_REFDATA_SOURCE);
if(!url.endsWith("/")) {
url += "/";
}
url += Constants.UPDATE_CUSTOMER_FILE;
CSVReader in = new CSVReader(new InputStreamReader(url.openStream()));
...//read and parse file here
}
catch(MalformedURLException e) {
msgId = R.string.error_invalid_base_url;
}
catch(UnknownHostException e) {
msgId = R.string.error_unknown_host;
}
catch(FileNotFoundException e) {
msgId = R.string.error_file_not_found;
}
catch(IOException e) {
msgId = R.string.error_reading_data;
}
catch(MyParseException e) {
msgId = R.string.error_invalid_file_format;
}
catch(Exception e) {
msgId = R.string.error_other_error;
}
finally {
try { in.close(); } catch(Exception e2) {}
}
// then I display AlertDialog using msgId as the message
正如你所看到的,我已经赶上几个例外 - 一些,我知道可以扔掉,一些我在测试中遇到的。我还需要迎合哪些其他例外情况?请注意,下载的数据量非常小(最多15-20 Kb),因此OutOfMemoryError
等应不适用。
我已经测试是否有使用连接管理器的连接,我才开始整个下载过程,所以这是覆盖。我将添加SocketException,谢谢。 – 2013-02-11 13:00:59