2013-10-22 83 views
0

我對Java相當陌生,而且我試圖讓自己的頭腦解決異常問題,以及何時應該使用它們。我一直在使用它們作爲錯誤檢查的一種形式,但我遇到了幾個人說,異常應該只用於程序控制之外的事情,比如用戶錯誤。我應該怎麼做而不拋出異常?

我有一個函數,對於2維的給定線,計算xMin和xMax之間的所有y值。如果行是垂直的,這個函數會拋出一個異常,因爲不可能在垂直行上計算y的所有值。在兩個y值之間也有一個等價的函數查找點,如果該行是水平的,則會引發錯誤。

findPointsInRangeOfX (int xMin, int xMax) throws LineIsVerticalException { 
    // check if line is vertical 
    // if vertical, throw an exception 
    // else calculate the y values for each integer between xMin and xMax 
    // return y values 
} 

我調用此函數作爲查找在給定窗口內的線,由最小和最大x和y值給出的所有的點的一部分。在這個函數中我不檢查這條線是否垂直;相反,我依靠findPointsInRangeOfX中的檢查並在該方法周圍使用try和catch塊。

pointsInWindow (int xMin, int xMax, int yMin, int yMax) { 
    try { 
     // Find list of all the points between xMin and xMax. 
     // Remove from list all points not between yMin and yMax 
    } 
    catch (LineIsVerticalException e) { 
     // If line is vertical, cannot find points between xMin and xMax 
     try { 
      // Instead, find list of all points between yMin and yMax 
      // Remove from list all points not between xMin and xMax 
     } 
     catch (LineIsHorizontalException e) { 
      // This part will never be reached because the line is vertical 
      // But the compiler complains if the exception isn't caught 
     } 
    } 
} 

可以嗎?我沒有拋出異常 - 因爲有一條垂直線沒什麼問題 - 但是我用它來告訴pointsInWindow它需要找到y值之間的點而不是x值。我是否應該複製檢查以查看該行是否在pointsInWindow函數中是垂直的而不是使用try catch塊?如果我確實複製了支票,我應該一起擺脫LineIsVerticalException嗎?

+2

您可能會發現一些關於何時拋出異常有趣http://stackoverflow.com/questions/77127/when-to-throw-an-exception以及這堆流量的因爲這與其他材料的鏈接http://stackoverflow.com/questions/15542608/design-patterns-exception-error-handling –

回答

5

你必須堅持單一責任原則:每一種方法都會做一件事。 現在你的方法做了兩件事:檢查它是垂直還是水平並計算一些東西。

另請注意:不要使用程序流的異常。

你應該分裂它的東西是這樣的:

bool isVertical(parameters){} 
bool isHorizontal(parameters){} 
SomeClass CalculateVertical(parameters){} 
SomeClass CalculateHorizontal(parameters){} 

你的程序流程看起來是這樣的:

if(isVertical(something)){ 
CalculateVertical(something); 
else if (isHorizontal(something)){ 
CalculateHorizontal(something); 
} 

實現示例:

SomeClass CalculateVertical(something){ 
if(!isVertical(something)) { throw new IllegalArgumentException() } 
// Calculations 
} 

注意,此異常不一定要被程序員抓住。

+1

同意。我會很懶,只是拋出IllegalArgumentException。這是一個現有的異常,並沒有被選中。它會強制用戶在嘗試調用方法之前驗證輸入。 – Kayaman

+0

所以我應該在CalculateVertical中假設我已經有了一條垂直線,並且沒有任何檢查方法? – Emma

+0

同意@Kayaman –

1

,或者你可以修改象下面這樣:

pointsInWindow (int xMin, int xMax, int yMin, int yMax) { 
    try { 
     // Find list of all the points between xMin and xMax. 
     // Remove from list all points not between yMin and yMax 
    } 
    catch (LineIsVerticalException e) { 
     // If line is vertical, cannot find points between xMin and xMax 
     // do something.. 
     } 
    catch (LineIsHorizontalException e) { 
     // unless LineIsVerticalException is superclass of LineIsHorizontalException, 
     // this will work 
     // do something .. 
     } 
    } 
} 
1

總的來說,我嘗試使用了意想不到的問題(如網絡故障),而不是僅僅邊境情況例外。然而,它是一個模糊的區別,取決於您的具體應用環境。

特別爲您的問題。怎麼樣分割功能。創建兩個函數來檢測給定行是水平的還是垂直的(例如boolean isVertical());.

在您的pointsInWindow函數中,您可以首先檢查您是在處理垂直/水平線的特殊情況,還是不要進一步調用findPointsInRange方法。

儘量不要複製邏輯,因爲這違反了DRY原則DRY principle,並且在維護代碼時往往會導致進一步的問題。

希望這有助於 馬庫斯

相關問題