This code duplication can be avoided by using a single, generic exception used application-wide. This solution involves the creation of a single DomainException<T> class with one or more serialization exception details classes:
[Serializable] public class DomainException<T> : Exception { public DomainException(T exceptionDetails) : base(default(String)) { Details = exceptionDetails; } public T Details { get; private set; } public DomainException() { // no argument constructor required for serialization } public DomainException(T exceptionDetails, Exception innerexception) : base(default(String), innerexception) { Details = exceptionDetails; } protected DomainException(SerializationInfo si, StreamingContext sc) : base(si, sc) { if (si == null) throw new NullReferenceException("si"); Details = (T)si.GetValue("exceptionDetails", typeof(T)); } }This exception class acts as a container for another simple property-only class that includes the exception details:
[Serializable] public class MyExceptionDetails { public MyExceptionDetails(int failureCode) { FailureCode = failureCode; } public int FailureCode { get; private set; } }A specific error details class like this one would be created where, in the past, an entire exception would have been created. Only the [Serialization] attribute is required to make it serialize-able/compatible with DomainException<T>. Throwing this exception would look like:
throw new DomainException<MyExceptionDetails>(new MyExceptionDetails(1001));And because the exception is a concrete type it can be explicitly caught:
try { // some code here } catch(DomainException<MyExceptionDetails> ex) { // examine ex.Details }
No comments:
Post a Comment