Tuesday, 15 November 2016

Exception and Exception handling (Part-1)

Que. What is an Exception?
Ans. Generally, We are saying exception is runtime error. But it is           not exactly exception is runtime errors. First of all try to                  understand what is an exception. Whenever we are developing        an application, then becomes two types of errors.

       1. Compile time error
       2. Runtime error.
  
Compile time Error
--------------------------
   The error that occurs in the program due to syntactical mistake is called compile time error. Syntactical mistake means forget the end statement through ;(semi colon), Curly braces ({}) are not in proper, Strings are not in double quotes etc... .These all things are called syntactical mistake and these mistakes under the compile time errors.
Compile time errors are not dangerous because compiler recognized the syntax error and give the list of mistake that's why compile time error is not dangerous.

Runtime error
-------------------
    The error that occurs in the code due to various reasons. These errors becomes not at the time of compilation of the code. These errors come at the time of execution of our application. Means execute the code and error will come is called runtime error.
Now, Why we get runtime error?
 There are various reasons to get runtime errors.

   => Wrong implementation of logic

         Ex:- Suppose 
         
                  int[] Objarr= {15,25,10,40,35}
                  for(int i=0;i<=Objarr.Length;i++)
                    {
                        Console.WriteLine(i);
                     }
         
         Here above the example we can see array declare withe length of five and in the for loop  logic execute in six times. So, here wrong implementation of logic is used.

   => Wrong input Supplied
         Ex:- We make an application and in the application have the input field string and numeric both and any one put in string field numeric value and in the numeric field string value at that time wrong input supplied.

   => Missing required resource. etc...
        Ex:- Suppose we have an application to read a resource like web cam, printer, projector etc... and when we start the application and at that time these resources are not detected by the application in this situation we got a runtime error Missing required resources.

Note:- Runtime errors are very dangerous because it is abnormal termination of the program. Whenever an error occurs inside the code the statement terminate and rest of the code is not execute that's why runtime error is very dangerous. 

Abnormal termination:

------------------------------
  Abnormal termination means when execute the code and error come inside the code, then application terminate that particular line of code without executing the rest of the line.

Ex:- Suppose we have a program for division:
        
        using System;
         class Demo
         {
           public static void Mai()
            {
               Console.Write("Enter 1st Number");
               int x=int.parse(Console.ReadLine());               
               Console.WriteLine("Enter 2nd Number");
               int y=int.parse(Console.ReadLine());
               int z=x/y;
               Console.WriteLine("Result :"+z);
               Console.ReadLine();
            }
         }

In the above program suppose if error becomes in the line of int x=int.parse.......; then program abnormally terminated without executing of rest of the code and give the runtime error. This is called abnormally termination.  

=> Now, In your mind hit one question, Who is the responsible for abnormal termination when ever runtime error occurs inside the code ?
So, the answer is Exception is a class which is responsible for abnormal terminating the program whenever errors occurs inside the program.
Whenever errors occurring in the program, then immediately exception classes comes into the picture for abnormal termination of the program.
There are so many exception classes are present, which is display different-different messages in different scenario. These are as follows:- 
  - IndexOutOfBoundException
  - DivideByZeroException
  - OverflowException
  - FormatException   etc....

These are the classes responsible for abnormally terminated the program whenever errors occur in the program.

Que: Why so many Exception classes are their?
Ans: Because in different-different scenario error message will                  become in different ways and these exception classes describe          about the error message in which scenario errors is occurs.               There are representing each and every error one class has been         defined. 
Here above mention exceptions are pre-defined exception classes.
Predefined classes means these are available in our library.

No comments:

Post a Comment