Creating custom exceptions

Creating custom exceptions
This tutorial will show you how to create your own exceptions by using a class derived from System.ApplicationException.

This is the continuation of the tutorial named Handling and throwing exceptions. I recommend you read that tutorial first.

In that tutorial, at the end you saw how to throw your own exceptions. Here will do the same thing, but a bit more complicated because now you’ll be able to customize the exception.

Create a new Console Application project named customExceptions in Microsoft Visual Studio .NET.

First we’ll have to create the class derived from System.ApplicationException:

// Creating the class derived from System.ApplicationException

public class NumberZeroException : System.ApplicationException

{

   // The constructor that takes msg as a parameter

   public NumberZeroException(string msg) : base(msg)

   {

   }

}

As you can see, it’s nothing more than a class derived from System.ApplicationException with a constructor that takes the msg as a parameter.

Here’s Class1 where we actually throw the exception if the user enters ‘0’.

class Class1

{

   [STAThread]

   static void Main(string[] args)

   {

      // Prompt the user to enter an integer

      Console.WriteLine("Please input an integer: ");

      // Try this block of code

      try

      {

         // Store the input in x

         int x = Convert.ToInt32(Console.ReadLine());

         // If the number entered is 0...

         if(x == 0)

         {

            // ...throw our custom exception with the following message parameter

            throw new NumberZeroException("You are not allowed to enter number 0");

         }

         // Show what the user entered (if it ain't 0)

         Console.WriteLine("You entered: " + x.ToString());

      }

      // Catch the custom exception

      catch(NumberZeroException e)

      {

         // And display the error message

         Console.WriteLine("Error: " + e.Message);

      }

   }

}

The code is fully commented so I suppose you have no problem understanding it.

Now let’s compile it and enter ‘0’ when prompted:

Please input an integer:

0

Error: You are not allowed to enter number 0

Press any key to continue

That’s all.

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top