Domanda

I read that finally key make a try-catch block final work, even function throw exception or not. But I wonder what is different if I don't put a code inside a finally block (like Function_2 below), which is the way I'm using to coding. Thank You!

void Function_1()
{
    try
    {
        throw new Exception();
    }
    catch
    {
    }
    finally                                   //Have finally block
    {
        Other_Function();
    }
}

void Function_2()
{
    try
    {
        throw new Exception();
    }
    catch
    {
    }

    Other_Function();                        //Don't have finally block
}
È stato utile?

Soluzione

if I dont put a code inside a finally block (like Function_2 below)

if you don't put code inside finally , until unless you won't get an exception that code block will be executed.

but if you get an exception before that code block (which was not kept inside finally) will not be executed as the control returns from there itself.

but if you keep your code in finally , it will be executed irrespective of the situation.

Example:1 without finally block

 try
    {
    //throw exption
    }
    catch
    {
    //return from here
    }

    //below statement won't get executed
    Other_Function(); 

Example:2 with finally block

  try
    {
    //throw exption
    }
    catch
    {
    //return from here if finally block is not available 
    //if available execute finally block and return
    }
    finally
    {
    //below statement surly gets executed
    Other_Function(); 
    }

Altri suggerimenti

A finally block, refers to a block of statements that is always executed, regardless of unexpected events or exceptions that may occur during an application's execution. The execution of a finally block is intended to release resources, such as database connections, which are usually available in limited quantities.

From MSDN:

Usually, when an unhandled exception ends an application, whether or not the finally block is run is not important. However, if you have statements in a finally block that must be run even in that situation, one solution is to add a catch block to the try-finally statement. Alternatively, you can catch the exception that might be thrown in the try block of a try-finally statement higher up the call stack. That is, you can catch the exception in the method that calls the method that contains the try-finally statement, or in the method that calls that method, or in any method in the call stack. If the exception is not caught, execution of the finally block depends on whether the operating system chooses to trigger an exception unwind operation.

Code in finally blocks is always executed. Finally provides a construct for ensuring the correct execution of programs. It ensures a block of statements are always reached before the enclosing method is exited. his program shows how the finally clause is part of the control flow in programs. In this program, a random number is generated. This value is used to determine whether to throw an exception, immediately return, or do nothing.

using System;

class Program
{
    static void Main()
    {
    try
    {
        // Acquire random integer for use in control flow.
        // ... If the number is 0, an error occurs.
        // ... If 1, the method returns.
        // ... Otherwise, fall through to end.
        int random = new Random().Next(0, 3); // 0, 1, 2
        if (random == 0)
        {
        throw new Exception("Random = 0");
        }
        if (random == 1)
        {
        Console.WriteLine("Random = 1");
        return;
        }
        Console.WriteLine("Random = 2");
    }
    finally
    {
        // This statement is executed before the Main method is exited.
        // ... It is reached when an exception is thrown.
        // ... It is reached after the return.
        // ... It is reached in other cases.
        Console.WriteLine("Control flow reaches finally");
    }
    }
}

Source

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top