-1
我有一個場景,我需要根據他的主要身份/替代身份找到訂戶。拋出指定異常或處理通用異常包含錯誤代碼
現在可能存在操作失敗的情況。像DBDown,未找到用戶,未找到替代身份的主要身份等。
現在要處理這種情況,我有兩個選項。
爲每個或一組故障情況創建特定例外。
public class SubscriberFetchingFailedException extends Exception { public SubscriberFetchingFailedException(String message) { super(message); } } /** * throw when Primary identity not found against alternate identity */ public class PrimaryIdentityNotFound extends SubscriberFetchingFailedException { public PrimaryIdentityNotFound(String message) { super(message); } }
你可以比較IOException異常和FileNotFoundException異常。
僅創建一個例外,並且對於每個場景,我提供一個錯誤代碼。
public class SubscriberFetchingFailedException extends Exception { public SubscriberFetchingFailedException(String message, int errorCode) { super(message); } }
你可以用的SQLException進行比較。
我有最多10個錯誤的情況。
現在我想知道哪一個更好的方法。