Search Forum
(57415 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


              
Printable Version

Handling Exceptions in C#
By J.V.Ravichandran

Exceptions are errors that may occur during the run-time of a program. Exception handling, thus, becomes an integral part of programming. In C#, exceptions are handled via the Exception base class. You can also create your own Exception class but, which will, necessarily, derive from the base, inbuilt Exception class.

An exception is, for example, a "divide by zero operation", which will result in a faulty or a needless result and hence, needs to be handled in a program. An exception can also be, "IndexOutOfBounds", which arises out when your program tries to access a non-existent element in an array say, 101, in an array of max size 100. There are many such inbuilt exceptions that are handled by the Exception base class but, since logic is about possibilities, the possibility of an unhandled exception may arise and to know how to create your own exception class to handle such a calamity is the purpose of this article.

The following is a program that defines my own Exception class.

using System;
public class MyException:Exception
{
public string s;
public MyException():base()
{
s=null;
}
public MyException(string message):base()
{
s=message.ToString();
}
public MyException(string message,Exception myNew):base(message,myNew)
{
s=message.ToString();// Stores new exception message into class member s
}
public static void Test()
{
string str,stringmessage;
bool flag=false;
stringmessage=null;
char ch=' ';
int i=0;
Console.Write("Please enter some string (less than 27 characters) - ");
str=Console.ReadLine();
try{
ch=str[i];
while (flag==false)
{
if (ch=='\r')
{
flag=true;
}
else{
ch=str[i];
i++;
}
}
}
catch(Exception e){
flag=true;
}

if (i>27)
{
stringmessage="You cannot enter more than 27 characters !";
throw new MyException(stringmessage);
}
}
public static void Main()
{
try
{
Test();
}
catch(MyException e)
{
Console.WriteLine(e.s);
}
}
}
The above program creates a new exception class called MyException, which derives from the base Exception class. The class has three constructors - one, default and two overloaded constructors. The intention is simple - to be able to overload a constructor of the base class, the base class' default or existing constructors must be implemented in the deriving class and then overload the relevant constructor. But, the real purpose of the class is to handle an exception - if the user enters more than 27 characters, then a message prompting the user of the error is to be displayed. Although, this is not really an exception, such an instance is actually called Validation of data but, for want of an example, such an occurrence has been used for exception handling. The new exception MyException is thrown from the Test() method and the message - "You cannot enter more than 27 characters !" - is passed to the class, which is retrieved by the catch block in the Main() method !