Pages

Wednesday, August 21, 2013

5 common exceptions in c# and their solution in .net


In this article we will see how to handle few very common exceptions in C#. Before start with example let’s learn what is exception exactly!

Exceptions are one kind of event that occurs when something goes wrong. And when it will go wrong no one knows. For example “Stack overflow exception” , Your program in working perfectly with small amount of input but somehow if it gets large number of input then “Stack overflow exception” might happen. But again we don’t know when program will get huge amount of input. Let’s start with few simple examples.

1 ) Null Reference Exception


This exception occurs when we try to work with null value with some function when that particular function is not capable to handle null String or null value. Have a look on below code to understand properly.


using System;
namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {

            try
            {
               
string String = null;
               
Console.WriteLine(String.ToString());
            }
            catch (NullReferenceException)
            {
               
Console.WriteLine("NullReferenceException!");
            }

            Console.ReadLine();

        }
    }
} 

2) Argument out of range Exception


This exception occurs when we try to access any element with it’s but index is not present in collection. In below example we are trying to access 10 th element but we have added only one element into it.


using System;
using System.Collections;
namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {

            try
            {
               
ArrayList list = new ArrayList();
               
list.Add(1);
               
Console.WriteLine("Item 10 = {0}", list[10]);
            }
            catch (ArgumentOutOfRangeException x)
            {
               
Console.WriteLine("ArgumentOutOfRangeException Handler");
            }
            Console.ReadLine();

        }
    }
} 

3) Overflow exception


This type of exception may occur when we try to convert one data type to another data type where target variable is not capable to store whole result. In below example we are using check keyword to check overflow of conversion.


using System;
using System.Collections;
namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {

            int Integer = 1234500;
            long Long = Integer;

            try
            {
               
long c = checked(Long * 99999999999999);
            }
            catch (OverflowException e)
            {
               
Console.WriteLine("Type cast overflow exception");
            }
            Console.ReadLine();

        }
    }
} 

4) Divide by zero exception


It’s one kind of Arithmetic exception which occurs when we try to divide a number with zero. Theoretically the result is infinitive but in computer programming it’s not allowed.   


using System;
using System.Collections;
namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {

            try
            {
               
int y = 0;
               
y = 10 / y;
            }
            catch (DivideByZeroException e)
            {
               
Console.WriteLine("Devide by zero exception");
           }
            Console.ReadLine();

        }
    }
} 

5 ) SQL Connection


SQL connection is one of the common exceptions in any application. It happen when there is something wrong with database connection. In below example we did not assign connection string but tried to open connection, hence SQL exception has occur.


using System;
using System.Collections;
using System.Data.SqlClient;
namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {

            try
            {
               
SqlConnection con = new SqlConnection();
               
con.Open();
            }
            catch (Exception e)
            {
               
Console.WriteLine("SQL connection error");
            }
            Console.ReadLine();

        }
    }
} 
 

No comments:

Post a Comment