Posts Tagged ‘exception’

When programming we should have 1 based index

December 21st, 2020

It is a bold statement.

Well… is it?

IIRC the most usual, unwanted, exception we get is null reference error.
The second is off-by-one error.

The off-by-one error is the result of us programmers on remembering to subtract one from an index, or subtracting once too many.

Alas we should have 1 based index, like natural language, and let the computer do the subtraction.

What…? That is not how computers work!


But that is how Humans work.

It is many years since we stopped arguing that a computer wants its data layed out such and therefore we adopt to it. Today we try to solve business problems, not computer problems (your domain may vary). We use higher level language. We name our variables after the domain. We use domain language.

We do not ask the customer for the zeroth invoice. And when we ask for the 2nd invoice we expect the 2nd invoice in a list.

The reason for 0 based index is, I believe, a historical technical reason.

When programming assembler, it is easy to point to a memory position and then add an index. Hence having a zero based index is a natural thing; we solve a computer problem.
Then C, a higher level language, comes along. It is way above assembler but is till constructed to be close to the physical machine to be easy to implement on any processor. By that time we still solved computer problems and keeping a zero based index was probably a no brainer.

Then came Java and C#. They try to look like C/C++ due to reasons of not-thinking-things-through and it-is-easier-to-not-move-the-cheese. They keep the zero based index even though they didn’t have to. So now we are stuck with it.

Until either 1) we invent a new operator

or 2) invent a yet new language.

Until then: Keep spreading the word that we should have a 1 based index.

Exception in AspnetMvc with Razor: Parser Error Message: Expected “}”.

April 28th, 2012

The below text/error can occur if you have missed one of the } in jquery in a @section features{ section.

It can be hard tracking exactly which } is missing because commenting out the javascript code doesn’t change anything – it seems the razor engine doesn’t care about that type of comments.  Instead start the @section{} anew and copy-paste the code in again piece by piece.

In my case it was a totally ok javascript for loop:

for (i = 0 ; i < 12 ; i++) { }

Event though it is correct syntax it makes Razor fail.  Seems to be a bug in AspnetMVC/Razor/Dotnet4.5/Visualstudio11.
I am losing karma by not making the bug easy to reproduce and report the bug.

Server Error in ‘/myApplication’ Application. 


Parser Error 

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: Expected “}”.

Source Error:

Line 204:        </p>

Line 205:    </section>

Line 206:</section>


How to design exception hierarchies

March 1st, 2012

I proudly copied the head line from the orginial article since I couldn’t make it any better.

The link below leads to a lengthy, but still to the point, text about how to design your exceptions.  The examples are from dotnet but the design can be used in, say, Java too.

( I used to have a design pattern for exceptions but I gave it up some time ago and thankfully never blogged about it. )

Honour those who should.

Exception: Dapper throws Value cannot be null. Parameter name: con

July 18th, 2011

If you are using Dapper and try to select data into an object and don’t have a default constructor the exception

Value cannot be null.
Parameter name: con

might be thrown.

The stack is:

[ArgumentNullException: Value cannot be null.
Parameter name: con]
   System.Reflection.Emit.DynamicILGenerator.Emit(OpCode opcode, ConstructorInfo con) +9566558
   Dapper.SqlMapper.GetClassDeserializer(IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) in C:\pathtomyproject\SqlMapper.cs:1177
   Dapper.SqlMapper.GetDeserializer(IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) in C:\pathtomyproject\SqlMapper.cs:674
   Dapper.<QueryInternal>d__4`1.MoveNext() in C:\pathtomyproject\SqlMapper.cs:397
   System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +327
   System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
   Dapper.SqlMapper.Query(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in C:\pathtomyproject\SqlMapper.cs:348


Exception: Unable to cast object of type ‘ASP._Page_Views_Home_Index_cshtml’ to type ‘System.Web.IHttpHandler’.

March 27th, 2011

This exception might have to do with the browser calling http://localhost/Views/Home/ instead of http://localhost/Home in an aspnetmvc project.

Also make sure there is a HomeController in /Controllers.

I haven’t delved into this since it just was an obstacle in something else I was playing with.

Exception: Only parameterless constructors and initializers are supported in LINQ to Entities.

October 30th, 2010

I am not sure of the *exact* meaning and reason for this but I found something that can be good to know.  Or not.

return context.NuggetSet.Where(n => n.Name == name).Select(x => new MyClass(x)).ToList();

threw a

Only parameterless constructors and initializers are supported in LINQ to Entities.

When inserting a ToList it worked as suspected/wanted.

return context.NuggetSet.Where(n => n.Name == name).ToList().Select(x => new MyClass(x)).ToList();

My guess is that by calling ToList we losen EF’s grip of the entities and can do what we want with it.


July 12th, 2010


}catch( Exception exc ){
 throw exc;

since it destroys the exception stack.

Instead write

}catch( Exception ){

If you don’t understand what “destroying the exception stack” means you probably don’t want to destroy it and this recommendation is even more something to heed.

To get to the exception use $exception.

$exception and $ReturnValue

July 12th, 2010

In dotnet one can write


which gives two debugging tools.  The first is the ability to set a break point on the catch line.

Note how I haven’t written catch( Exception exc) but only catch( Exception).  This makes us avoid a compiler warning because we don’t use the exc variable.  Which brings us to trick number two in Visual studio: Since we don’t have a variable with the exception instead write $exception in the immediate window or the quickwatch window.  $exception brings forward the [hidden] exception reference.

Also note how there is no variable behind the throw statement.  This makes the framework hide the catch/throw and leave the stack intact.

Update: In VisualStudio2010 it looks like it is enough to write exception in the quickwatch window.

With dotnet 4.5.1? one can write $ReturnValue the same way to get to the… the… the… return value!

Unable to open the physical file Operating system error 5: "5(failed to retrieve text for this error. Reason: 15105)". (Microsoft SQL Server, Error: 5

October 18th, 2009

Try to attach a database with Management Studio when using Windows 7 and SQLServer 2008 developer edition and one might get an error message similar to the title of the article.

Whether this is a bug or not is beyond my knowledge but the solution/workaround is to connect as SA and not as windows authentication.