Prabir's Blog

where the tech matters...

Portable Bash (msys - mingw)

September 10
by prabir 10. September 2009 22:50

I usually like running some software as portable. It allows me to easily use the same configurations across various computers.

One of the software that I use is a bash shell from called msys. I have attached the zip file which can be downloaded at the and of the post.

Note: It only contains the core shell and no other stuffs such as the binutils and C/C++ compilers.

msysCORE-1.0.11-20080826_portable.zip (2.88 mb) [Downloads: 141]

Tags:

compiler | Linux | software

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

Coco/R for Visual Studio RC0

June 08
by prabir 8. June 2009 19:40

As some of you might have known I have been working on LLVM# Compiler. I have decided to use Coco/R as the scanner and parser generator. In order to make my work easier, I had landed up creating a custom tool for Coco/R. It automatically generates necessary scanner and parser files when I save the Coco/R Attributed grammar file. So if you plan to work on creating a compiler or using Coco/R I am sure you are going to like it.

image

Instructions on how to install and use can be found at codeplex (http://cocor.codeplex.com).

In the upcoming articles I will be posting on how to generate those files using custom tools.

Tags: ,

compiler

LLVM# – Native C# Compiler

May 19
by prabir 19. May 2009 22:49

Right now I’m quite busy working on with my senior project called LLVM# compiler. Its main objective is to be able to write the code in C# and execute the code as a native C or C++ application by converting the C# code to LLVM (Low Level Virtual Machine). This project will tend to remove the dependency of C# from the .net framework, the mono runtime or the portable.net runtime engine. The project is being discussed at the moment but if you want to have a look at what’s happening, please do check the official website of LLVM# compiler at http://projects.prabir.me/compiler.

For more info on LLVM click here.

Tags: , ,

C# | compiler | LLVM#