下面是代碼異常沒有被handeled
@SuppressWarnings("MissingPermission")
public static Location getLocation() throws LocationNotFoundException{
mFusedLocationProviderClient.getLastLocation()
.addOnSuccessListener(myActivity, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if(location!=null) {
mLocation = location;
}
else {
throw new LocationNotFoundException();
}
}
});
return mLocation;
}
現在有些線路,LocationNotFoundException
是一個自定義異常,延長了Exception
類。在我拋出自定義異常的行中,我收到一個錯誤消息,說明LocationNotFoundException
未處理,即使我使用throws
關鍵字讓異常沿着調用堆棧向下傳播,但我正在處理異常,調用特定的方法,使用try/catch塊。
這裏是我處理異常的地方。
try {
mLocation = LocationUtil.getLocation();
} catch (LocationNotFoundException e) {
Toast.makeText(MainActivity.this, "Location Not found", Toast.LENGTH_LONG)
.show();
}
似乎無法弄清楚我在這裏做錯了什麼。希望有人能幫助我解決這個問題。
檢查您的自定義LocationNotFoundException是否與具有相同名稱但不同包的另一個異常不衝突 – MatPag
@MatPag不,似乎沒有異常名爲LocationNotFoundException –