They wrote errata

Sadly, early editions of the official Exam Ref book for 70-483 Programming in C# contain considerable errors. The errata can be downloaded from the Microsoft Press website.

I have the latest version of the book (5th printing, April 2015) and even this isn't perfect. Objective 1.5 - Implement exception handling - contains an example of a custom exception. Listing 1-98 is supposed to show a bespoke exception with numerous constructors - but unfortunately contains references to other, unrelated classes. Here is a corrected version, the comment is taken from the MSDN C# Programming Guide.

using System;
using System.Runtime.Serialization;
 
[Serializable]
public class OrderProcessingException : ExceptionISerializable
{
    public OrderProcessingException(int orderId)
    {
        OrderId = orderId;
        this.HelpLink = "http://www.someurl.com";
    }
 
    public OrderProcessingException(int orderId, string message)
        : base(message)
    {
        OrderId = orderId;
        this.HelpLink = "http://www.someurl.com";
    }
 
    public OrderProcessingException(int orderId, string message, Exception innerException)
        : base(message, innerException)
    {
        OrderId = orderId;
        this.HelpLink = "http://www.someurl.com";
    }
 
    // A constructor is needed for serialization when an
    // exception propagates from a remoting server to the client.
    protected OrderProcessingException(SerializationInfo info, StreamingContext context)
    {
        OrderId = (int)info.GetValue("OrderId"typeof(int));
    }
 
    public int OrderId
    {
        get;
        private set;
    }
 
    public override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        base.GetObjectData(info, context);
        info.AddValue("OrderId", OrderId, typeof(int));
    }
}

Comments