DESIGNING .NET 4
WINDOWS APPLICATIONS:
STABILITY AND MAINTENANCE

- ERROR HANDLING

EXCEPTION HANDLING

STRATEGY

Error Messages

To Users

MULTI-TIER

EXCEPTION HANDLING

Database to

Data Access Layer

Business Logic Layer

to Presentation Layer

<serviceBehaviors>

<behavior name="EmployeeManager_Behavior">

<serviceDebug includeExceptionDetailsInFaults="true" />

</behavior>

</serviceBehaviors>

"The server was unable

to process the request

due to an internal error."

[DataContract(Namespace="http://Microsoft.ServiceModel.Samples")]

public class MathFault

{

private string errorMessage;

public MathFault(string message)

{

this.errorMessage = message;

}

[DataMember]

public string ErrorMessage

{

get { return errorMessage; }

set { errorMessage = value; }

}

}

[OperationContract]

[FaultContract(typeof(MathFault)]

int Divide(int n1, int n2);

[ServiceContract]

public class CalculatorService
{

[OperationContract]

int Divide(int a, int b)

{

// Throw the FaultException to send the fault to the client.

if (b == 0)

throw new FaultException<MathFault>(new MathFault("Divide by zero"));

return a/b;

}

}

PROCESSING UNHANDLED

EXCEPTIONS

Global Exception

Handler

CUSTOM EXCEPTION

CLASSES