Replace Exception with Test(以测试取代异常)
生活随笔
收集整理的這篇文章主要介紹了
Replace Exception with Test(以测试取代异常)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
面對一個可以預先檢查的條件,卻拋出了一個異常
public double getValueForPeriod(int periodNumber) {try {return values[periodNumber];} catch (ArrayIndexOutOfBoundsException e) {return 0;} }重構:調用函數之前先做檢查
public double getValueForPeriod(int periodNumber) {if (periodNumber >= values.length) {return 0;}return values[periodNumber]; }總結
以上是生活随笔為你收集整理的Replace Exception with Test(以测试取代异常)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 死锁Waiting--DeadLockD
- 下一篇: 2021年高考成绩查询山东德洲,2021