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

Simplest way of creating custom exceptions in .NET using Visual Studio 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

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.

4 Comments to “Simplest way of creating custom exceptions in .NET using Visual Studio 2015”

  1. 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.