public void GetContain()
{
try{
str.contains("KeyWord");
catch(NullPointerException ex)
{
throw new NullPointerException ();
}
}
public String executeHttpGet(String Url) {
HttpClient httpClient = new DefaultHttpClient();
String result="";
try{
HttpResponse response = httpClient.execute(request);
if(response.getStatusLine().getStatusCode()==HttpURLConnection.HTTP_OK)
{
result = EntityUtils.toString(response.getEntity());
}
}
catch(NullPointerException ex)
{
throw new NullPointerException ();
}
finally
{
httpClient.getConnectionManager().shutdown();
}
return result;
}
以上的code 在出現例外時會丟出 NullPointerException
然而這樣子的寫法使用者實際上並不知道發生甚麼事情
到底NullPointerException是由誰丟出來的呢?
比較好的寫法應該像第二個範例
public void GetContain()
{
try{
str.contains("KeyWord");
catch(NullPointerException ex)
{
throw new DataAccessException("Get Contain Fail",ex);
}
}
public String executeHttpGet(String Url) {
HttpClient httpClient = new DefaultHttpClient();
String result="";
try{
HttpResponse response = httpClient.execute(request);
if(response.getStatusLine().getStatusCode()==HttpURLConnection.HTTP_OK)
{
result = EntityUtils.toString(response.getEntity());
}
}
catch(NullPointerException ex)
{
throw new DataAccessException("Http Request Fail",ex);
}
finally
{
httpClient.getConnectionManager().shutdown();
}
return result;
}
以上的code可以很清楚的知道例外是由誰產生
這樣的好處 讓我們將原本無法理解的exception變成我們所能理解exception
讓我們知道確切的問題點在哪
接著下面的例子是將第三方API可能產生的exception 利用單一例外處理使得我們更好管理
將所有的例外使用單一的例外(DataAccessException)去處理
由於第三方的API 出現例外時不管錯誤為何
通常處理的方式都會類似
所以可以使用這樣的方式來處理
可以將該 API 包成一個class 專門處理該API
ThirdPartyHandler{
private ThirdPartyAPI ThirdParty;
public ThirdPartyHandler(ThirdParty)
{
this.ThirdParty = ThirdParty;
}
public String executeThirdPartyAPI(String Url) {
try{
result = ThirdParty.Execute();
}
catch(NullPointerException ex)
{
throw new DataAccessException(ex);
}
catch(IOException ex)
{
throw new DataAccessException(ex);
}
finally
{
...
}
return result;
}
}
從呼叫者角度定義例外類別告訴我們
利用封裝自定義 exception或者是增加message 讓我們更輕易的管理例外
沒有留言:
張貼留言