Internals of Exception Handling

Exceptions are runtime bugs that occur due to illegal action taken by the user. The Exception occurs whenever the object state is corrupted and which causes the program to terminate. Based on the severity of the exception we choose either to continue with the program by proper handling the code or rethrow the exception again to the parent block and eventually terminate the whole program.

In this post, I am not going to talk about how to throw/re-throw exceptions or even write the best practices on Exception handling, but I will go on with some of the hidden facts of exception handling which you might want to know and later on will involve IL to check how exceptions are generated.

The Basics

Exception handling is one of the weakest section of CLR. Even though I like most of the things that CLR brings to us, but I definitely disagree if the exception handling is one amongst it. Even the exception handling as been evolved with the system is enhanced very well recently with the introduction of Uncatchable Exceptions, RuntimeWrappedException, code contacts etc. Lets discuss some of the major enhancements to the exception system.

The .NET exception handling is made up of three sections :

  1. try : In this segment you need to write only the portion which can potentially throw errors. 
  2. catch : This section can overload based on the type of Exception and will be used to handle the exception which occurred in the Try block.
  3. finally : This block executes irrespective of try and catch. We write the clean up tasks here as it ensures to run even anything occurs. 

Shout it Submit this story to DotNetKicks Bookmark and Share
Read Disclaimer Notice

Internals to C# iterators

Hi Folks,

Hope you all are good.  If you are new to this post, I would recommend to red my other post too from the Internals series. In this series I am trying to cover the basic C# programming and also relate them with the compiled MSIL. In my previous post while I am going through with the internals of foreach loop I told you to cover more on iterators in my next post. It is time to cover the basis on which the C# IEnumerable stands and the iterators.

If you ask me why I like to work on C# not VB.NET or other languages, I would point to some of the flexibilities that I get with C#. Even though in VB.NET vNEXT, iterators are coming into being but still C# is the primary language which introduced yield.

In this post I am going to demonstrate the basic feature behind the C# iterators and also introduce you the secret behind the yield keyword of C#.

Shout it Submit this story to DotNetKicks Bookmark and Share
Read Disclaimer Notice

Different approaches to Casting

Conversion from one type to another is the most common programming need of our daily activities. We need cast operation almost for all applications we create. Few of them require regular type conversion while other requires you to specify the cast on an expression. In BCL, there are few libraries available to you which might help you to do this casting for you. All these are very specific to a situation and the choice between them is the one most of you are confused about. In this post, I will try to cover all of these scenarios and let you rethink it before writing conversions.

Implicit Type Conversion

This is the most basic conversion in which the type of the data is modified without any prior truncation of existing data. The implicit type conversion is automatic and does not require any special treatment to the instruction set. The implicit type conversion occurs when a variable of small integral type is stored into a reference of larger integral type or a derived class object is stored in base class reference. As for example

int i = 10;
long l = i;

Here the target time is larger in size than the source. Hence there is no probability of producing a truncation or a leak in this case and the compiler allows this conversion.

At the same time this rule is also valid for base – derived types where the derived class object can be cast easily to the base class as base class reference is lower in object hierarchy than derived class objects.

IEnumerable<object> lst = new List<object>();

Explicit Type Conversion

In case of explicit type conversion, the object is not converted automatically but it needs an explicit cast expression before the assignment. This cast is evaluated at runtime and compiler will generate an exception if the cast is not valid. The explicit cast can sometimes produce data truncate as either a large integral value is tried to store into a smaller type or base class object is tried to store in derived reference. For instance,

Shout it Submit this story to DotNetKicks Bookmark and Share
Read Disclaimer Notice

Internals of loops (While, For and ForEach)

Practically speaking, a loop is the primary building block of a program. We use loop to repeat a set of actions for a certain interval. Now if you think of these intervals, it could be a traversal from one number (called as start index) to another number (called as end index). Very often or probably out of 10 such loop eight times you loop a collection such that you start from 0 and loop until you point to the end of the sequence.

In C# (and VB.NET) we use while, do-while, for and foreach loop to loop through a set of instructions. In this post I will try to demonstrate the basic loops for a while and later on take on a bit about foreach loop and its requirement and finally go deep into its internals.

If you want to see all my Internal Series, you are welcome to follow the link :
Internals to .NET

Shout it Submit this story to DotNetKicks Bookmark and Share
Read Disclaimer Notice

Internals of Extension Methods

Hi folks,

It is nice to see people likes my post so much. Ever since I wrote about “Internals of Delegates” there is a large hits on my site and people wanted me to continue posting few more internals like that. Even I was thinking the same, I was confused what I should start for my next topic. Finally, I have decided to speak about another important C# feature that we use very often called Extension Methods and try to look deep into its facts. Lets first demonstrate the basics of Extension methods first and later we will dive deep into it. If you are comfortable with Extension Methods, please skip the following section.

The Basics

Extension method is actually defined as static but acts as an instance specific method. In other words, even though the code that is running inside the Extension method is not declared within the scope of the class, it is still associated with an instance of an object for that particular class. Extension method does not need to be in the same namespace where the actual type is. Hence you can easily add extension method anywhere outside the namespace and any other static class. The namespace which when added to the code will automatically add those extension methods and call appropriately for an existing type. Extension methods can also be thought as extension to a pre-existing type.

Shout it Submit this story to DotNetKicks Bookmark and Share
Read Disclaimer Notice

Query Notification to .NET Application (SQLDependency & SQLCacheDependency)

SQL Server 2005 comes with a new concept which lets you to get automatic notifications to your client application whenever some data is changed in your database. The Query Notification and SQL Server Service Broker helps you in doing so. In this post I am going to show you how you could utilize this concept to build your own query notifiers for your own client application.

Introduction

It is somewhat a common practice for an application working with real-time day is to poll the database server every now and then to get the updates. We generally poll the database using a background Timer and update the data whenever new data comes in. Hence we loosen up a lots of resources while calling the database so much and even ruin our application overall performance.

The idea of this post is to let you understand the basics of Query Notification and let you through by building an application that gets automatic notification from the SQL Server itself and update the UI. SQLDependency is a class provided with .NET Base class library which enables you to get notification when SQLCommand changes its output.

I have posted an article demonstrating the concept clearly using some sample application one with a WPF windows client that gets notification alerts instantly and another an ASP.NET application that invalidates cache based on the Dependency.


http://cid-bafa39a62a57009c.office.live.com/self.aspx/.Public/NotificationMessages.zip
http://cid-bafa39a62a57009c.office.live.com/self.aspx/.Public/CacheDependencyNotifier.zip





Read the Entire Article.
Shout it Submit this story to DotNetKicks Bookmark and Share
Read Disclaimer Notice

Proud to be a Microsoft MVP in Client App Dev on Jan 2011

Hi Friends,

First of all Happy New Year 2011 to everybody.

Its been a long journey to the technical world. I have always tried to sharpen my knowledge as time progresses and also wanted you to know more about technology. I am very much inclined towards technology and in love with .NET products.

January 1st, 2011 is been the most eventful day in my life.  I got a mail from Microsoft, that I have been selected as Microsoft "Most Valuable Professional" in "Client Application Development". Its like a dream come true and is my biggest award of life. Its priceless.


Shout it Submit this story to DotNetKicks Bookmark and Share
Read Disclaimer Notice

Author's new book

Abhishek authored one of the best selling book of .NET. It covers ASP.NET, WPF, Windows 8, Threading, Memory Management, Internals, Visual Studio, HTML5, JQuery and many more...
Grab it now !!!