Simplest way of creating custom exceptions in .NET using Visual Studio 2015

By | January 1, 2015

.NET framework supports all kinds of exception types which are sufficient for most of the code bases, in some cases it may require to define custom exceptions in our applications. These may be just because of manage the overall exception handling in a simple way,  make the code more readable with respect to solution or even in some cases because of design approach.  Any Custom Exception that we create needs to derive from the System.Exception class. We can either directly derive from Exception or ApplicationException as a base class.  Creating these exceptions class, derive them form base class and define the structures takes a while. Using Visual studio 2015, you can do a very quickly and can take a quick preview before you change in your codebase.

You can start this by just typing the custom exception name as shown in below.

Creatng a Custom Exception

Creatng a Custom Exception

As the “MyCustomException” is not a known type, Visual Studio won’t be able to recognize this and would let you take control over it or else it will show it as error. You can Ctrl+. or “Alt+Shift+F10” , to generate the stub for the type you have defined.

 Must Read Tip: Generate Method Stubs using Shortcut Key in Visual Studio

With the Ctrl+. / Alt+Shift+ F10 pressed, Visual Studio will show the list of options along with the preview of  all possible combination of file / class placement along with the code snippet. As we have use the type name with “throw new”, Visual Studio is smart enough to understand that, this is an exception class and creates the new class by just derives the same from Exception as base class. The Quick Preview Window in Visual Studio 2015 shows how the code block would looks like.

Exception Class Code Snippet Preview

Exception Class Code Snippet Preview

 

Just select the first option, and press Enter. Visual Studio will add the “MyCustomException.cs” in the solution explorer.

 

Added New File in the Porject

Added New File in the Project

 

Open the “MyCustomException.cs” file and you will find following code snippet ready for you.

[Serializable]
internal class MyCustomException : Exception
{
public MyCustomException()
{
}

public MyCustomException(string message) : base(message)
{
}

public MyCustomException(string message, Exception innerException) : base(message, innerException)
{
}

protected MyCustomException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}

You can go ahead and just simply write the logic for your expectation.

P.S : Adding new files from the undefined types is not new in VS 2015, it was available from prior version of Visual Studio as well. Refer to this post Generate Method Stubs using Shortcut Key in Visual Studio

4 thoughts on “Simplest way of creating custom exceptions in .NET using Visual Studio 2015

  1. Srinivasan

    If you don’t mind, Can I use this post in my article as reference?

  2. pity 2014

    If some one needs to be updated with most recent technologies
    therefore he must be go to see this web page and be up to date everyday.

Comments are closed.