不要问,在我的法典中是否正确处理例外情况,那么,有人会给我一些想法,谈谈我下面的法典。
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);
}
}
this method is part of my business logic and calls the method above
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){
}
}