exceptions

These are like exceptions in the normal course that we set for ourselves. Similarly, when you write programs, unforeseen problems may arise during its normal path of execution.
These unforeseen problems are nothing but an euphemism for errors. Just as in life, in the programming world, these errors can be further classified into Fatal errors and Non-Fatal errors. A Fatal error is an error that brings the program to a grinding halt. A Non-Fatal error is an error that allows your program to run but with limited capacity. This can be exemplified by the following.Let's assume you have a card that is not of a high resolution. Accordingly, your browser displays your page in a lower resolution. Now, technically, that is an error but it is not a Fatal one. However, if you didn't have a graphics card at all then it would be a Fatal error. Thus, we may also call an unforeseen problem or error an Exception. In other words, therefore, the word Exception is used almost synonymously with the word Error.Earlier, the problem was that we never centralized error handling. Let's assume you have to open three files. Each time you open a file you have to check whether an error occurred or not. So you have to conduct that check for every file. Since there are three files, it would mean repeating the same error check thrice. That is surely a waste of time. Or you could be calling two functions and checking for the same error in both the functions. One reason that programmers don't write error-handling routines is that they get tired of the mundane task. It is the same thing repeated over and over again.









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

namespace ConsoleApplication13
{
class Cons
{
public static string ReadString(string st)
{
Console.Write(st);
return Console.ReadLine();
}
public static Int32 ReadNumber(string st)
{
Console.Write(st);
return Convert.ToInt32(Console.ReadLine());
}
}


class MyException : Exception
{
string ErrorMessage;
public MyException(string ErrorMessage)
{
this.ErrorMessage = ErrorMessage;
}
public override string ToString()
{
return ErrorMessage;
}
}


class Program
{
static void Main(string[] args)
{
int n1 = Cons.ReadNumber("Enter First Number :");
int n2 = Cons.ReadNumber("Enter First Number :");
int n3;
try
{
n3 = n1 / n2;


throw new MyException("This is my Exception");

// throw new IndexOutOfRangeException();

Console.WriteLine(" Result of Division {0}", n3);
}
catch (DivideByZeroException)
{
Console.Write("The second number must not be zero");
}
catch (ArgumentOutOfRangeException)
{

}
catch (IndexOutOfRangeException)
{
Console.Write(" Index out of range exception");
}
catch (MyException e)
{
Console.Write(e.ToString());
}
finally
{
Console.WriteLine("After try block .");
}



}
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: