Click here to Skip to main content
Click here to Skip to main content

Tagged as

Try - finally alternative

, 8 Jun 2011 CPOL
Rate this:
Please Sign up or sign in to vote.

What do you think about using block with delegate instead of finally? Does shorter code make better code? Smile | :)

using(Finalizer finalizer = new Finalizer( () => Console.ReadKey())
{
   //Code
}

or:

using(Finalizer finalizer = new finalizer())
{
    Action = () => Console.ReadKey();
    //Code
}

Alternative to:

try
{
    //Code
}
finally
{
   Console.ReadKey();
}
Here is a simple implementation of Finalizer:
public class Finalizer: IDisposable
{
 
   /// <summary>
   /// Gets or sets the action.
   /// <summary>
   /// <value>
   /// The action.
   /// <value>
   /// <summary>
   public Action Action { get; set; }
 
   /// <summary>
   /// Initializes a new instance of the <see cref="Finalizer"/> class.
   /// <summary>
   public Finalizer() { }
 
   /// <summary>
   /// Initializes a new instance of the <see cref="Finalizer"/> class.
   /// <summary>
   /// <param name="action">The action.<param>
   public Finalizer(Action action)
   {
      this.Action = action;
   }
 
   /// <summary>
   /// Performs application-defined tasks associated with freeing,
   /// releasing, or resetting unmanaged resources.
   /// <summary>
   public void Dispose()
   {
      if (Action != null)
      Action.DynamicInvoke();
   }
}
 
And thanks to some posts i realized some other benefits from using this code.
 
If your scenario is:
 
try
{
   //code
}
finally
{
   try
   {
       //code
   }
   catch(Exception)
   {
   }
}
 
you can simplify it and make it reusable with writing dispose method of finalizer like this:
 
/// <summary>
/// Performs application-defined tasks associated with freeing,
/// releasing, or resetting unmanaged resources.
/// <summary>
public void Dispose()
{
   try
   {
      if (Action != null)
         Action.DynamicInvoke();
   }
   catch(FileNotFoundException) { }
   catch(FileLoadException) { }
}

License

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

Share

About the Author

Tomáš Vepřek

Czech Republic Czech Republic
No Biography provided

Comments and Discussions

 
GeneralRe: Aye i wouldnt want that. PinmemberTomáš Vepřek13-Jun-11 22:16 
GeneralRe: Sure, but would it be clear to have that in a seperate Class... PinmemberNaerling10-Jun-11 10:28 
GeneralRe: Well take for example a WCF service and ServiceHost object. ... PinmemberTomáš Vepřek9-Jun-11 22:47 
GeneralRe: But do you really want business logic in the Finally? To me ... PinmemberNaerling8-Jun-11 8:34 
GeneralI agree that in scenario where you want whole code in try ca... PinmemberTomáš Vepřek7-Jun-11 23:05 
GeneralWell this is exactly what i wanted to hear :) I dont want t... PinmemberTomáš Vepřek7-Jun-11 22:57 
GeneralReason for my vote of 3 See previous comment. PinmemberNaerling6-Jun-11 21:00 
GeneralHow is this any shorter? The way you present it it seems sho... PinmemberNaerling6-Jun-11 20:59 
GeneralRe: Well think of what you can do with this, you can for example... PinmemberTomáš Vepřek7-Jun-11 23:00 
GeneralReason for my vote of 3 Bad idea for reasons already mention... Pinmembertgrt6-Jun-11 12:56 
GeneralReason for my vote of 1 (1) My reason is the same as Jeremy'... PinmemberMarcus Kramer6-Jun-11 9:13 
GeneralReason for my vote of 1 It's not catching the the exception.... PinmemberJeremy Hutchinson6-Jun-11 4:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411028.1 | Last Updated 8 Jun 2011
Article Copyright 2011 by Tomáš Vepřek
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid