Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C#
class MainClass
{
        static void Main()
        {
        try {
            int[] arrobj = new int[2];
            arrobj[0] = 45;
            arrobj[1] = 54;
            arrobj[2] = 43;
        }
        catch(Exception e) {
            Console.writeline(e.Message);
            Console.readline();
        }
        Console.writeline("Code executes without Finally");  //code executes without finally
        Console.readline();
    }
}


class MainClass
{
        static void Main()
        {
        try {
            int[] arrobj = new int[2];
            arrobj[0] = 45;
            arrobj[1] = 54;
            arrobj[2] = 43;
        }
        catch(Exception e) {
            Console.writeline(e.Message);
            Console.readline();
        }
        finally {
            Console.writeline("executes inside Finally");  // Code executes with finally
            Console.readline();
        }
    }
}

What is the use of finally block ? The same code in the finally block executes without using finally block.
Posted
Updated 30-Sep-12 20:09pm
v2

Hi,

The use of finally block is to execute a block of code even if there is an exeception in catch block. For example.

C#
try
{
   //some code that throw exception and
   //in your code you are using some unmanaged resource like opening DB connection, file etc
}
catch(Exception ex)
{
   //handle exception but while writing log it is throwing exception
   
}
finally
{
   // you should clean unmanaged resouce in finally block because it gaurantees to execute.
}


An other example.

if you are using return in try block

C#
try
{
//some code and then
  return something;
}
catch(Exception ex)
{
}
finally
{
}
//No code will be executed after the finally block if return statement is avail in try block.
 
Share this answer
 
v2
Comments
Aarti Meswania 1-Oct-12 2:35am    
correct answer! 5+
 
Share this answer
 
The finally block is always executed, no matter what. This may be helpful when throwing exceptions or leaving your function earlier.
See this sample which also calls finally
C#
try {
  //do something
  return;
}
finally {
  Console.writeline("executes inside Finally");  // Code executes with finally
  Console.readline();
}

Or this one:
C#
try {
            int[] arrobj = new int[2];
            arrobj[0] = 45;
            arrobj[1] = 54;
            arrobj[2] = 43;
        }
        catch(Exception e) {
            Console.writeline(e.Message);
            Console.readline();
            throw;
        }
        finally {
            Console.writeline("executes inside Finally");  // Code executes with finally
            Console.readline();
        }

In both cases the finally block is executed, but "normal" code after the catch handler/the return is not executed. So, in your case it may not make very much sense, but in the two samples from me they make sense.
See here for some more explanations:
http://msdn.microsoft.com/en-us/library/zwc8s4fz%28VS.80%29.aspx[^]
Hope that helps!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900