我有這樣的事情:不能捕捉異常
...
while (itr.hasNext()) {
...
try {
if (userInput.equalsIgnoreCase(theData.getName())
|| (country.getName().toString()).equalsIgnoreCase(theData.getName()))
{
result = new Country(PointT, dist);
break;
} else {
for (int i = 0; i < repl.size(); i++) {
if (repl.get(i).toString().contains(theData.getName())) {
theName = true;
result = new Country(PointTt, dist, theName);
break;
} else if (theParts.get(i).toString().equalsIgnoreCase(theData.getName())) {
result = new Country(PointT, dist);
break;
}
}
}
} catch (NoSuchElementException ex) {
logger.error("Does not exist in database", ex);
throw ex;
} catch (NullPointerException ex) {
logger.error("Does not exist in database", ex);
throw ex;
}catch (Exception ex) {
logger.error("Does not exist in database", ex);
throw ex;
}
}
...
所以,我用theData.getName()
相比,userInput
這僅僅是一個字符串,country.getName()
這是也是一個字符串,repl.get(i)
這是一個List<String>
,theParts.get(i)
這也是一個List<String>
。
我找不到解決方案來完成這項工作。在數據庫中不存在字符串(theData.getName())
時拋出異常。
讀取數據
爲了讀取theData
:
我使用FileInputStream fis = new FileInputStream(filelocation);
然後,只提取的字符串,並將其儲存:
ArrayList<String> theNames = new ArrayList<String>();
,然後我將theNames
與其他迭代器合併並創建
ArrayList<theNamesLoc> theNamesLocList = new ArrayList<theNamesLoc>();
而這正是我申請了數組列表:
Iterator<TheNamesLoc> itr = theNamesLocList.iterator();
while (itr.hasNext()) {
..}
if(!next())throw ..?類似的東西?你不顯示你如何閱讀你的數據,那麼我們應該怎麼知道? – Stultuske
是數據null?你在使用前先檢查它嗎? – Assafs
if(theData == null)如何拋出xyzException? – procrastinator