Exception handling in C Sharp

Exception handling in C Sharp

Exception handling the C Sharp language exception handling features provide a way to deal with any unexpected or exceptional situation that arise while a program is running is exception handling uses that try catch and finally keyword to attempt action that may not succeeded to handle failure and the cleanup resource afterwards.

Exception handling in C Sharp  

Exception can be generated by the Common Language Runtime (CLR) by third party library or by the application code using the throw keyword.

In C#.net I have an exception class this is base class in system namespace exception class handle the all kind of exception during application execution.

Properties of Exception class

Data: Get a collection of key value pairs that provide additional user defined information about the exception.
HelpLink: Gets or sifts a link to help file associated with this exception.
HResult: Gets or sets HResult,a coded numeric value that is assigned to a specific exception. 
InnerException: Gets the exception instance that caused the current exception.
Message: Gets a message that describe current exception. 
Source: Gets or sets the name of application or the object that causes the error.
StackTrace: Getting representation of the immediate frame on the call stack.
TargetSite: Gets the method that through the current exception.



How to use Exception in programming

1.Try Block
2.Catch Block
3.Finally Block
4.Throw Keyword

Try Block:

try
{
   //statement that can cause Exception
}

Catch Block:


catch(Exception Ex)
{
   //statement to handle the exception
}


Finally Block:


finally
{
  //Statement to clean Up
}

Throw Keyword:


catch(Exception Ex)
{
   throw(e)
}


Some Important points about Exception Handling

1.Exception are types that all directly or indirectly derived from System.Exception class.
2.Exception object contains detailed information about the error, such as the state of the call stack and a text description of the error.
3.A try block is used to throw multiple exception that can handle by using multiple catch blocks.
4.More specialized catch block should come before a generalized one otherwise a catch block will never be executed.
5.Exception can be generated by a program by using the throw keyword.
6. If no exception handling mechanism is used, the program stops executing with an error message.
7.By providing a catch block without a brackets or arguments, we can catch all exception occurred inside a try block.



A simple program of Exception Handling


using System.Linq;
using System.Text;
using System.Threading.Tasks;


    class Program
    {
        static void Main(string[] args)
        {
            int num1 = 100;
            int num2 = 0;
            try
            {
                Console.WriteLine("Division of {0} by {1} is : {2}", num1, num2, num1/num2);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }


Attempted to divide by zero.   
Press any key to continue . . .




In this program occurs an exception (DivideByZeroException) in  Try Block and Catch Block handle the exception. And display the message of the Exception.


Also Read:








EmoticonEmoticon