The below are the few design rules for creatng custom exceptions.
1. Derive exceptions from System.Exception or one of the other common base exceptions. As per "Catching and Throwing Standard Exception Types" guidelines you should not derive custom exceptions from "ApplicationException".
2. End exception class names with "Exception" suffix. Just follow consistent naming conventions.
3. Make exceptions serializable. An exception must be serializable to work correctly across application domain and remoting boundaries.
4. Provide at least the common constructors on all exceptions.
5. Try to avoid deep exception hierarchies.
Custom exceptions greatly improve and simplify the error handling and also increase the overall code quality.
The first step is to create the class for your custom exception that inherits from the base exception class. Here we have created "ZipCodeNotFoundException" class.
ZipCodeNotFoundException.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace CustomExceptions
{
[Serializable]
public class ZipCodeNotFoundException : Exception, ISerializable
{
public ZipCodeNotFoundException()
{
// Add implementation
}
public ZipCodeNotFoundException( string message ) : base( message )
{
// Add implementation
}
public ZipCodeNotFoundException( string message, Exception inner ) : base( message, inner )
{
// Add implementation
}
protected ZipCodeNotFoundException( SerializationInfo info, StreamingContext context ) : base( info, context )
{
// Add implementation
}
}
}
To use the custom exception, we are checking whether the given input zipcode is available in the list or not. If it's not available we are
throwing the custom "ZipCodeNotFoundException" exception. In the main method we are catching the "ZipCodeNotFoundException" exception and writing the exceptiond details in the console.
ZipCodes.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CustomExceptions
{
public class ZipCodes
{
private IList<string> _zipCodes;
private void Load()
{
_zipCodes = new List<string>();
_zipCodes.Add("90001");
_zipCodes.Add("90002");
_zipCodes.Add("90003");
_zipCodes.Add("90004");
_zipCodes.Add("90005");
_zipCodes.Add("90006");
}
public bool VerifyZipCode(string zipCode)
{
Load();
return _zipCodes.Contains(zipCode) ? true : false;
}
}
}
Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CustomExceptions
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Enter ZipCode:");
string zipcode = Console.ReadLine();
string result = FindZipCode(zipcode);
Console.WriteLine(result);
Console.ReadLine();
}
catch (ZipCodeNotFoundException ex)
{
Console.WriteLine(ex.GetType().FullName);
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
private static string FindZipCode(string zipcode)
{
ZipCodes zipCodes = new ZipCodes();
if (zipCodes.VerifyZipCode(zipcode))
{
return zipcode + " Found!";
}
else
{
throw new ZipCodeNotFoundException("ZipCode not found!");
}
}
}
}
The below is the output,
Available ZipCode Scenario :
Input : Enter ZipCode: 90001
Output: 90001 Found!
Not Available ZipCode Scenario :
Input : Enter ZipCode: 99999
Ouptut: CustomExceptions.ZipCodeNotFoundException
ZipCode Not Found