Saturday, December 6, 2014

Program to show about how to deal with Exceptions

7:56 PM - By azeheruddin khan 0

Description: - There are three different types of errors identified in a program:-
  1. Syntactical/grammatical mistakes done by the programmer in a program are called compile-time errors. These errors are detected during program compilation.
  2. If a program shows wrong output after program execution due to usage of wrong logic then, such kind of wrong logic is called Logical error.
  3. The error which abnormally terminates the normal flow of execution in a program at the line of code where it has occurred is called a runtime error. These runtime errors are also called as Exceptions as they represent some operations which cannot be possible to perform in reality. For ex: - DivideByZero. The word 'Exception' means something which does not conform to the standards. All exceptions are represented by a class : Exception
All the exception classes like ArithmeticException and FormatException are the child classes of Exception class. A “virtual” property called Message is defined in the Exception class to get the error message associated with an exception. Now each and every child class of Exception class can override this Message property as this property is declared as “virtual”. So, the reference of the child class of Exception class can call this Message property which is re-implemented in the child class.

Example:

public class Exception
{
   public virtual string message
   {
     get
       {
         return“<someerrormessage>”;
       }
    }
}

public class FormatException:Exception
{
   public override string Message
   {
     get
     {
        Return “input string was not in a correct format”;
     }
   }
}


To handle these exceptions we need to use try and catch blocks:-

try
{
    -statements which cause runtime errors.
    -statements which do not cause runtime errors.
}
  catch
{
    -statements which are to be executed when runtime errors occur
}

Now to handle these exceptions, we need to put the code which is responsible for causing runtime errors in try block. And the code which needs to be executed when runtime error occurs should be put in catch block.

So, when a runtime error occurs within a try block at a particular statement, the exception manager throws or raises an exception by creating an object of an appropriate exception class (i.e. FormatException or ArithmeticException) which represents that error. This object is caught by the appropriate catch block which handles that exception by displaying a user-friendly error message and then, the normal flow of execution in the program continues. Thus, the catch block is called as Exception Handler.
 
The finally block is used to execute a set of statements irrespective of whether an exception occurs or not.

Example:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExceptionHandlingExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Exceptions e = new Exceptions();
            Console.ReadLine();

        }
    }

    class Exceptions
    {
        int a, b, c;
        public Exceptions()
        {
            try
            {
                Console.Write("Enter the value for b:");
                b = Convert.ToInt32(Console.ReadLine());
                Console.Write("enter the value for a:");
                a = Convert.ToInt32(Console.ReadLine());
                c = b / a;
            }
            catch (ArithmeticException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (FormatException e)
            {
                Console.WriteLine(e.Message);
            }

            finally
            {
                if (a > 0)
                {
                    Console.WriteLine("the result is " + c);
                }
                Console.WriteLine("please give correct input");
            }
        }

    }

}



Output:
































About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, Asp.net and ms sql.My work comprise of medium and enterprise level projects using asp.net and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

adsense

© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top