Prabir's Blog

where the tech matters...

Alternative Method of Throwing Exception

August 05
by prabir 5. August 2009 11:44

Most of us use Exceptional Handling mechanism to prevent our programs from crashing and then debug the program by understanding the message thrown.

Traditional way to use would be to use try..catch..finally block to handle the exceptions thrown using throw. How do you throw exceptions? There are basically two ways to throw exception.

throw new Exception();

Or simply,

throw;

Most of us would be using the first method to throw. But sometimes, it might not contain enough information that is required to solve our problems (bug). In that case, the second method would be more useful.

Below is an example of the c# code that would help to distinguish between the two types of errors.

There are two methods defined in the Program class under ConsoleApplication1 namespace. (I’m assuming the file named a.txt doesn’t exist in c drive)

The first method uses throw ex;

 

private void LoadFile()
{
    try
    {
        using (FileStream fs = new FileStream("c:\\a.txt", FileMode.Open, FileAccess.Read))
        {
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

 

The second method uses only throw; (You can enter this only in the catch block).

 

private void LoadFile2()
{
    try
    {
        using (FileStream fs = new FileStream("c:\\a.txt", FileMode.Open, FileAccess.Read))
        {
        }
    }
    catch (Exception ex)
    {
        throw;
    }
}

 

The program is as follows:

using System;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{

    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();

            try
            {
                p.LoadFile();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }

            Console.WriteLine();

            try
            {
                p.LoadFile2();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);

            }
        }

        private void LoadFile()
        {
            // code omitied.
        }


        private void LoadFile2()
        {
            // code omited.
        }
    }
}

When you run the program you will get the following output.

image

The error message printed to the console is as follows:

   at ConsoleApplication1.Program.LoadFile() in D:\Prabir\Documents\Visual Studi
o 2008\ConsoleApplication1\ConsoleApplication1\Program.cs:line 47
   at ConsoleApplication1.Program.Main(String[] args) in D:\Prabir\Documents\Vis
ual Studio 2008\ConsoleApplication1\ConsoleApplication1\Program.cs:line 17

   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, I
nt32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions o
ptions, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
   at ConsoleApplication1.Program.LoadFile2() in D:\Prabir\Documents\Visual Stud
io 2008\ConsoleApplication1\ConsoleApplication1\Program.cs:line 62
   at ConsoleApplication1.Program.Main(String[] args) in D:\Prabir\Documents\Vis
ual Studio 2008\ConsoleApplication1\ConsoleApplication1\Program.cs:line 28
Press any key to continue . . .

 

 

As you might have noticed the second information is in more details. This allows you to know your stack trace in more details.

So how is it different?

In the first method, the stack trace is initialized at throw ex statement. The second method rethrows the exception meaning that the previous stack trace is kept along with more information where they occurred.

ThrowRethrowExample_CSCode.zip (538.00 bytes) [Downloads: 83]

Tags: , ,

.NET Framework | ASP.NET | compiler

Microsoft’s way of N-Tier system

July 23
by prabir 23. July 2009 16:05

For those of you who have not know about Microsoft’s solution to the N-Tier system, you should hurry up and learn more about it. Its already been months old with two public releases (Preview versions).

Microsoft calls it .NET RIA services. What the hell is it and how is it different from traditional 2-tier and 3-tier systems?

Microsoft .NET RIA Services simplifies the traditional n-tier application pattern by bringing together the ASP.NET and Silverlight platforms. The RIA Services provides a pattern to write application logic that runs on the mid-tier and controls access to data for queries, changes and custom operations. It also provides end-to-end support for common tasks such as data validation, authentication and roles by integrating with Silverlight components on the client and ASP.NET on the mid-tier.

Untitled

I have posted some links below to help get familiarize with .NET RIA services.

Installing July 2009 preview: Download the installer from here. (Prerequisites: Visual Studio 2008SP1 or Visual Web Developer SP1 and Silverlight 3.0 Runtime,SDK and Tools. You are required to uninstall March / May 2009 Preview before installing the newer version.

More information/tutorials on it can be found at

  1. http://code.msdn.microsoft.com/RiaServices
  2. http://blogs.msdn.com/brada/archive/2009/03/19/what-is-net-ria-services.aspx
  3. http://silverlight.net/forums/t/108916.aspx
  4. http://blogs.microsoft.co.il/blogs/bursteg/archive/2009/04/04/build-a-simple-application-with-net-ria-services-silverlight-3.aspx
  5. http://www.silverlightshow.net/items/Creating-applications-with-.NET-RIA-Service-Part-1-Introduction.aspx
  6. http://blogs.microsoft.co.il/blogs/bursteg/archive/tags/.Net+RIA+Services/default.aspx

Tags: , ,

.NET Framework | ASP.NET

.NET/E – .net/everywhere

May 25
by prabir 25. May 2009 21:13

For some of you out there who have been working on wpf or silverlight, you must have known the codename of Silverlight – WPF/E. It stood for WPF Everywhere, meaning, WPF could run on any platform or Operating System. Well the same applies to .NET also which I like to refer to it as .Net/E (just kidding). Even this project has similar goals. Its not about other .NET frameworks or engines like mono or portable.net, but rather takes a total different approach to making .NET apps run everywhere.

Unlike those traditional frameworks mentioned earlier, Mainsoft Grasshopper, takes a totally different approach by converting .NET codes to Java bytecode, thus allowing the code to run everywhere the Java runtime is installed. Since Java Runtime is installed in many of our computers, especially non Microsoft OS, our code could run on more platforms and OS without telling the user to install the mono framework. ( you need to provide easy access to the customers). To summarize, it about running your .NET code anywhere Java works. That’s a face to face challenge with Java.

For more details please have a look at it official website here.

You can download the plug-in for visual studio from following links. I’m just providing these links as alternatives.

For Visual Studio 2008 (52.8 mb) [Downloads: 2863]
For Visual Studio 2005 (56.7 mb) [Downloads: 824]
For Visual Studio 2003 (58.9 mb) [Downloads: 229]

I have also been working on a similar project for my bachelors degree, which I posted on my blog out here. Its about converting C# code to a portable code called LLVM that can be compiled and run in almost all sorts of platforms. For more information please check the official site of my project at http://projects.prabir.me/compiler.

Tags: , ,

Visual Studio | software | .NET Framework