Loading blog entries.. loading


There are totally 2 blogs under category: Programming».Net Framework»Essence

 

Exception handling particulars in C#

 
Written by Wayne Ye  Friday, July 8, 2011

Background

Exception handling appears in most .NET applications, this post is trying to describe some  Exception handling particulars in C# which might not take enough awareness from C# developers. 

Differences between throw and throw ex 

I guess every C# developer has seen code snippet below:

 try
{
    // Do some work, exception occurs
}
catch (IOException ex)
{
    // Exception caught, re-throw it to bubble up
    throw ex;
}

In the catch block, we can rethrow the caught exception instance of IOException to higher level, and we may also saw another way in a little different:

 try
{
    // Do some work, exception occurs
}
catch (IOException ex)
{
    // Exception caught, re-throw it to bubble up
    throw;
}

Is there any different? 

The answer is yes! To prove that I wrote a simple snippet of code below, first is a simple customized Exception: DummyException.

 internal class DummyException : Exception
{
    public DummyException(String dummymsg)
        : base(dummymsg)
    {

    }

    public DummyException(String dummymsg, Exception innerException)
        : base(dummymsg, innerException)
    {

    }
}

 And then I manually throw the DummyException within one method, while handle it in different way mentioned above:

 class Program
{
    private static void DoLowLevelOperation()
    {
        // Do some low level operation
        throw new DummyException("A dummy exception message!");
    }

    public static void MethodThrowException1()
    {
        try
        {
            DoLowLevelOperation();
        }
        catch (DummyException de)
        {
            throw;
        }
    }
    public static void MethodThrowException2()
    {
        try
        {
            DoLowLevelOperation();
        }
        catch (DummyException de)
        {
            throw de;
        }
    }

    static void Main(string[] args)
    {
        try
        {
            MethodThrowException1();
        }
        catch (DummyException de1)
        {
            Console.WriteLine(de1.Message);
            Console.WriteLine(de1.StackTrace);
        }

        try
        {
            MethodThrowException2();
        }
        catch (DummyException de2)
        {
            Console.WriteLine(de2.Message);
            Console.WriteLine(de2.StackTrace);
        }
    }
}

 The result will be:


View Post»



Permalink:http://wayneye.com/Blog/Exception-Handling-Particulars-In-Csharp
Tag:

Encrypt .Net Configuration file

 
Written by Wayne Ye  Friday, June 11, 2010

Under some scenarios the developers want to encrypt some sections inside app.config or web.config file, this article How To: Encrypt Configuration Sections in ASP.NET 2.0 Using RSA describes how to do so clearly, Scott Guthrie also posted one: Encrypting Web.Config Values in ASP.NET 2.0.

However, in the posts above they uses aspnet_regiis.exe and seems it doesn’t directly support app.config, if we want to encrypt app.config for Windows Form or WPF applications, while I tried use it to encrypt my app.config file, it generates a web.config which means my Winform definitely can’t use it, even if I copy the encrypted appSettings section from this generated web.config to my own app.config(ConfigurationManager.AppSettings[EncryptedKeyName] is null after I did that).

Config Encrypt


View Post»



Permalink:http://wayneye.com/Blog/Encrypt-DotNet-Configuration-File
Tag: