What is not a reason to throw a custom exception?
I recently took a brain test and got a pretty good result (something like 4.5, master's degree). I didn't know the answer to just one question (the rest I was sure, or at least thought I knew the correct answer :)). The question arises:
Which of the following is NOT the reason for creating custom exceptions?
Choice 1
Insert a strong label for later verification
Choice 2
To strongly specify the purpose of a specific exception
Choice 3
Allow remote serialization
Choice 4
Handle general steps when throwing an exception
Choice 5
Add custom properties to propagate custom data
I answered "4" for handling general steps when throwing an exception. Which one do you think is correct?
a source to share
Choice 3. The base exclusion either already supports remote access, or else you won't add remote access.
The "exception" Mark mentions in the comment is the following: I think this is not what the test authors had in mind:
In a WCF service, you can allow an unhandled exception to propagate outside of the service. WCF will turn it into a SOAP Fault, which may or may not contain the details of the unhandled exception, depending on the configuration.
Better would be to recognize certain sets of exceptions and deliberately translate them into SOAP Faults. For example, a service that operates on database objects might expect that the object is sometimes not found; sometimes trying to add a new entity will lead to duplication; sometimes an attempt to update would result in an invalid state. Such a service may decide to open NotFoundFault
, DuplicateItemFault
and InvalidStateFault
.
The service will define three errors as Data Contracts in order to determine their content:
[DataContract]
public class FaultBase {
[DataMember]
public string ErrorMessage {get;set;}
}
[DataContract]
public class NotFoundFault : FaultBase {
[DataMember]
public int EntityId {get;set;}
}
[DataContract]
public class DuplicateItemFault : FaultBase {
[DataMember]
public int EntityId {get;set}
}
[DataContract]
public class InvalidStateFault : FaultBase {
}
Then you specify that specific operations can return errors like this:
[OperationContract]
[FaultContract(typeof(NotFoundFault))]
public Entity GetEntityById(int id)
Finally, you can wrap the DAL exception in such a way that WCF will return a specific error:
try {
return DAL.GetEntity<Entity>(id);
}
catch (DAL.NoSuchEntityException ex)
{
throw new FaultException<NotFoundFault>(
new NotFoundFault {EntityId = ex.Id, ErrorMessage = "Can't find entity"});
}
I think the test developer was trying to get you to think that you need to do something special in order for the exception to be serialized for remote access to another AppDomain. This will not be the case if the custom exception was correctly implemented, since the .NET exception classes provided are serializable. Thus, being serializable is not an excuse for throwing a custom exception, since the base class must already be serializable.
a source to share
I do agree that 4 is wrong: to handle common steps when throwing an exception.
Exceptions do not have to take "steps" when they are thrown, but rather tell the system that an exception has been thrown for which the current class (and possibly a module) does not know how to handle.
If a function requires common steps to be performed correctly, that functionality should be included in the code itself, not split into an exception.
a source to share