2012-03-23 89 views
1

如何找出類似的錯誤是否是因爲FileNotFoundException,或EOFExceptionorNo Internet Access等更具體的錯誤情況....錯誤:IOException異常 - 如何獲得更具體的錯誤

下面

是我的代碼使用訪問互聯網,我得到的IOException,我知道這是由於沒有權限訪問互聯網..但我怎麼想知道爲確保這個錯誤是否與文件,或eof或權限有關......

下面是我的代碼和捕獲所有異常,但沒有人會給我一個特定的情況異常。

try { 
    HttpGet request = new HttpGet(MyConstants.URL); 
    HttpResponse response = null; 
    response = httpClient.execute(request); 
    //HttpResponse httpResponse = httpClient.execute(request); 
    HttpEntity httpEntity = response.getEntity(); 
    xmlList = EntityUtils.toString(httpEntity); 
} 
catch (MalformedURLException e) { 
    xmlList = "URL: is a malformed URL"; 
    e.printStackTrace(); 
} catch (UnsupportedEncodingException e) { 
    e.printStackTrace(); 
    xmlList = "URL: UnsupportedEncodingException"; 
} catch (ClientProtocolException e) { 
    e.printStackTrace(); 
    xmlList = "URL: ClientProtocolException"; 
} catch (SocketTimeoutException e) { 
    e.printStackTrace(); 
    xmlList = "URL: SocketTimeoutException"; 
} catch (ConnectTimeoutException e) { 
    e.printStackTrace(); 
    xmlList = "URL: ConnectTimeoutException"; 
} catch (IOException e) { 
    xmlList = "URL: IOException"; 
    e.printStackTrace(); 
} 

回答

0

抓住它!

} catch (FileNotFoundException e) { 
    xmlList = "URL: FileNotFoundException"; 
    e.printStackTrace(); 
} catch (EOFException e) { 
    xmlList = "URL: EOFException"; 
    e.printStackTrace(); 
} catch (IOException e) { 
    xmlList = "URL: IOException"; 
    e.printStackTrace(); 
} 

沒有「沒有互聯網接入」的例外。

+0

我知道有沒有這樣的例外,但我想知道'IOException'的原因,得到它? – 2012-03-23 03:00:26

+0

@AbuHamzah例外是它自己的原因。如果您抓到了'FileNotFoundException',那就是'找不到文件'。如果您發現'EOFException',那就是EOF。 – EJP 2012-03-23 05:52:59