Thursday, September 27, 2012

English 2 Bangla Dictionary

An awesome firefox addon for English to Bangla dictionary.


https://addons.mozilla.org/en-US/firefox/addon/english-2-bangla-dictionary/?src=userprofile 


 
I tested this at firefox 15.0.1 version.


I love it.


Wednesday, September 19, 2012

Get Inner most exception


Lets consider a simple example.

We have a static void Main() method which is the entry point of our application.
From static void Main() we call a method named AMethod()
And AMethod() intern calling an another method AMethod1().
Now lets say AMethod1() unfortunately raises an exception which is sent to AMethod() and same is propagated to Main() function.





Look at the following line of code.


class Program
{
static void Main(string[] args)
{
try
{
AMethod();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
}

private static void AMethod()
{
try
{
AMethod1();
}
catch (Exception ex)
{
throw new Exception("A Method", ex);
}
}

private static void AMethod1()
{
throw new Exception("Exception from A Method 1.");
}
}


Now, what will happen if you run the code? You will see that the output is "A Method"
Which is the exception message of AMethod() – which is not original error message. But as a developer our expected message is "Exception from A Method 1." Of AMethod1() – which is disguised by AMethod().

To avoid this situation the solution is GetBaseException() method which belongs to the Exception class and “returns the System.Exception that is the root cause of one or more subsequent exceptions.” – means the error from the main source which will not be omitted by another method.



class Program
{
static void Main(string[] args)
{
try
{
AMethod();
}
catch (Exception ex)
{
Console.WriteLine(ex.GetBaseException().Message.ToString());
}
}

private static void AMethod()
{
try
{
AMethod1();
}
catch (Exception ex)
{
throw new Exception("A Method", ex);
}
}

private static void AMethod1()
{
throw new Exception("Exception from A Method 1.");
}
}