[英]Exception handling execution flow
我有以下代碼。
當客戶不存在時,它執行 Throw Number 1。
執行繼續並到達 Throw Number 2。
所以顯示的消息最終是來自 Throw Number 2 的消息。
但我不希望這種情況發生。
當執行到達 Throw Number 1 時,它應該停在那里並且永遠不會到達 Throw Number 2,以便顯示來自 Throw Number 1 的消息。
怎么做?
public void updateCustomerName(int customerId, String name){
try {
//code to find customer by id here
if(customerExists.isEmpty() == false) {
//code to update customer name here
}
else {
//THROW NUMBER 1
throw new CustomerAPICustomException("Invalid customer id : " + customerId);
}
}
catch(Exception ex) {
//THROW NUMBER 2
throw new CustomerAPICustomException("Customer update error.");
}
}
您可以執行以下操作:
public void updateCustomerName(int customerId, String name){
if(customerExists.isEmpty()) {
throw new CustomerAPICustomException("Invalid customer id : " + customerId);
}
try{
//customer update
}
catch(Exception ex) {
//THROW NUMBER 2
throw new CustomerAPICustomException("Customer update error.");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.