Click here to Skip to main content
14,207,753 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Hi,

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

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

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.
   
v2
Comments
Aarti Meswania 1-Oct-12 2:35am
   
correct answer! 5+
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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
try {
  //do something
  return;
}
finally {
  Console.writeline("executes inside Finally");  // Code executes with finally
  Console.readline();
}

Or this one:
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!
   

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



Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190612.1 | Last Updated 1 Oct 2012
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100