It can create its own exception class for each DLL or even for each class. For each custom exception, create your own identifier so that it matches the description that is intended to be tracked and to manage messages displayed to users. An identifier or range of identifiers determines which friendly message to show users. That way, you can identify the exceptions that were caught when they were caught and processed, the exceptions that weren't caught and processed, custom exceptions, as well as track, classify, and find code that throws an exception and much more. To add a record to the mix, you can use log4net or common.logging (works with Entlib). This makes logging easier because it is easy to use and implement, and there is built-in functionality for logging exceptions with all the useful information and it can be used on a large scale.
ADDED: I think I might have had the wrong Base Exception class, and you will notice that DebugFormat and ErrorFormat are our modified log4net code, however I think you get the point.
namespace Playing.Service { public class UserService { private static readonly ILog log = LogManager.GetLogger(typeof(UserService)); public void SaveUser(string username, string firstName, string lastName) { try { Playing.Repository.UserRepository repository = new Repository.UserRepository(); repository.SaveUser(username, firstName, lastName); log.DebugFormat("Saved User Info"); } catch (Repository.RepositoryException rex) { log.ErrorFormat("Repository Could Not Save User Information: {0}\n Error Message: {1}\nStack Trace: {2}", rex.Message, rex, rex.StackTrace); throw new ServiceException(rex.Message, 2400); } catch (Exception ex) { log.ErrorFormat("Could Not Save User Information: {0}\n Error Message: {1}\nStack Trace: {2}", ex.Message, ex, ex.StackTrace); throw new ServiceException(ex.Message, 12400); } } } public class ServiceException : Playing.Common.BaseException { public ServiceException(string errorMessage) : base(errorMessage) {} public ServiceException(string errorMessage, int errorID) : base(errorMessage, errorID) {} public ServiceException(string errorMessage, params string[] args) : base(errorMessage, args) { } } } namespace Playing.Repository { public class UserRepository { private static readonly ILog log = LogManager.GetLogger(typeof(UserRepository)); public void SaveUser(string username, string firstName, string lastName) { try {
source share