[問題] 關於例外檢查的問題
最近在寫java時,在選擇用exception和條件式判斷時
不太明白兩者使用的時機
如果是用條件式判斷:
String type = getPara();
if( type != null && type.equals("haha") )
output += type;
else
output += "something wrong"
如果是用exception:
String type = getPara();
try{
if( type.equals("haha") )
output += type;
} catch (java.lang.NullPointerException e){
output += "something wrong"
}
這兩者雖然只有針對null pointer這邊做舉例
可是其實我還是有遇到其他更複雜的狀況
個人的想法是
如果我用條件式判斷時,可以掌握所有的情況,但是比較複雜
如果我用例外處理時,程式碼相對簡潔,但是不能掌握所有的判斷情況(?)
想問問各位有關這兩個做法的想法,如果有哪裡說錯了,還請大家幫忙糾正
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.130.36.2
→
11/15 16:57, , 1F
11/15 16:57, 1F
→
11/15 21:37, , 2F
11/15 21:37, 2F
→
11/16 10:57, , 3F
11/16 10:57, 3F
→
11/16 11:02, , 4F
11/16 11:02, 4F
→
11/16 11:03, , 5F
11/16 11:03, 5F
推
11/16 17:20, , 6F
11/16 17:20, 6F
討論串 (同標題文章)