Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Creating custom exceptions

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

On Wednesday, July 28th 2004 at 07:41 AM
By Andrew Pociu (View Profile)
****-   (Rated 3.4 with 27 votes)
Contextual Ads
More C# Resources
Advertisement
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.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Shankar on Tuesday, August 7th 2007 at 08:46 AM

Thank you, it helped me...

by Coralys.com on Friday, August 1st 2008 at 07:16 AM

A very simplistic approach that does not cover proper custom exception "manufacturing". In fact if you read Microsoft documentation you will see that they say you must derive your custom exceptions from System.Exception and NOT from System.ApplicationException.

You also missed here the default constructors and the serialization constraints.

But well, better than nothing at all.

by Structed on Tuesday, February 17th 2009 at 04:41 AM

Sorry to say that, but that's absolutely useless.
You can learn even more if you just use the Exception-Snippet in Visual Studio (Express)...

by software dev on Thursday, December 10th 2009 at 04:53 PM

"Where there is a will, there is a way"
If you have been in software development for a while, you would agree that criticizing any code block can be easy. Let's be a little positive and appreciate the efforts of the author.

by Structed on Friday, December 11th 2009 at 03:15 AM

I did not meant the code being bad or unclean.

But you are right. It's good to have someone telling the new developers directions.

Sorry for this post above. I have matured myself :-)

by mohit on Wednesday, March 24th 2010 at 06:56 AM

yes indeed it is helping enough

by KiNGPiN on Monday, April 19th 2010 at 08:56 AM

The simplest tutorial i found !! gr8 :)

by KiNGPiN on Monday, April 19th 2010 at 08:56 AM

The simplest tutorial i found !! gr8 :)

by Srikanth on Thursday, May 13th 2010 at 01:37 AM

Super mamau

by Coralys.com on Thursday, May 13th 2010 at 09:42 AM

If you don't provide constructive criticism developers are bound to commit (and enforce) errors over and over. There is much difference between a tutorial and a code snippet.

Anyway, if there are some that prefer to ignore the comments then sorry for the mistakes you are bound to commit now and in the future.

by KiNGPiN on Thursday, May 13th 2010 at 10:03 AM

True said , but it's the way a comment is made important

For me, it was the KISS(Keep it Simple Stupid) rule that made me like this Tutorial

by arifur rahman on Wednesday, April 6th 2011 at 07:59 AM

nice tutorial this help me to give security to my application. With the help of this tutorial i give domain level security. You can visit my blog for details information.

http://learneveryday.net/asp-net/creating-custom-exceptions-in-asp-net/

also visit my blog for <a href="http://learneveryday.net">
Php tutorials, cakePhp tutorial, CodeIgniter tutorial, jQuery tutorials, wordpress tutorial</a>

by Replica watches on Monday, November 21st 2011 at 12:34 AM

in comparison to spending twenty to thirty thousand on an original Swiss-designed model. In addition, your watch will have the same look and feel of a real watch, and you will have thousands of dollars in savings. This situation is ideal in the event that you need an impressive and stylish luxury timepiece from a world-class brand name, but are not in a position to afford to buy one.

by credit cards to build credit on Monday, December 12th 2011 at 05:34 AM

Use System.Windows.Forms.Cursor.Position to get the cursor position in screen coordinates.I admire the important information you offer within your content. I'll bookmark your web site and have my kids examine up the following typically.

by Margarita on Saturday, January 14th 2012 at 01:38 AM

Once you have recreated the problem and captured these steps, you can save them to a file and send it to your support person, who can then open it up and view

by gd poker online on Sunday, January 22nd 2012 at 03:19 PM

Hello.This article was extremely remarkable, particularly because I was looking for thoughts on this subject last Thursday.

by Doctores Pediatras on Monday, February 27th 2012 at 07:18 AM

Just read your post and would like to thank you for maintaining such a cool blog.

by Doctores Pediatras on Monday, February 27th 2012 at 07:18 AM

Just read your post and would like to thank you for maintaining such a cool blog.

by limousine whistler starlimousine.com on Monday, February 27th 2012 at 07:56 AM

Thanks for sharing this useful information! Hope that you will continue with the kind of stuff you are doing.


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C# Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia
Other Resources
Bargain EZ Tech Coupons & Deals