我想知道IM處理異常正確地在我的代碼是否因此希望有人能給我一些想法在我的代碼如下異常處理綜述
public IEnumerable<Job> GetAll() {
try {
return this._context.Jobs;
} catch (SqlException ex) {
//error so dispose of context
this.Dispose();
//wrap and rethrow back to caller
throw new CentralRepositoryException("Error getting all jobs", ex);
}
}
這個方法是我的業務邏輯的一部分,並呼籲上述
方法public IEnumerable<Job> GetAllJobs() {
try {
return this._jobsRepository.GetAll();
} catch (CentralRepositoryException ex) {
//logging code to go here
//throw back to caller
throw;
} catch (Exception ex) {
this._jobsRepository.Dispose();
//logging code to go here
//throw simple exception to caller
throw new CentralRepositoryException("A general exception has occurred");
}
}
我的自定義異常
public class CentralRepositoryException : System.Exception {
public CentralRepositoryException(string message, Exception innerException)
: base(message, innerException) {
}
public CentralRepositoryException(string message){
}
}
這看起來像一個代碼審查問題! – AksharRoop