Tuesday, August 14, 2007

Know Your Language: Try-Catch-Fun in C#

I am currently going through several interview-processes. During the last technical interview I realized, that my knowledge of my favorite programming languages is not as profound as I would like it to be. I always dismissed this shortcoming with the excuse, that such "low-level stuff" was not my primary concern. But now I remembered a quote from my German teacher:

Those without sophisticated vocabulary will stay unredeemed!

- Markus Lüdin

Thinking about the truth of this, I set out to learn to express myself: My first stop was the try-catch-playground in C#-world.

In the sandbox a toddler had built the following:

try{
  return true;
}
finally{
  return false;
}
image

I stopped and began to stare ... schizophrenia started to fill my head ... but fortunately my buddy, the C#-Compiler, stepped in and rebuked the toddler: This was not allowed!

But the toddler was not so easily intimidated: He dug and sculpted with his little hands, and as I stayed and watched, schizophrenia slowly crept back into my head:

try
{
  throw new ArgumentException("First Ex!");
}
finally
{
  throw new ApplicationException("Second Ex!");
}
image A inexplicable urge came over me, and I started to build fervently my own constructs in the sand around those of the toddler:
[Test]
public void First_Exception_Is_Lost()
{
  SchizophreniaTrap trap = new SchizophreniaTrap();
  try{
      trap.TryFinallyException();
  }
  catch(ArgumentException ex){
      Debug.WriteLine("Caught First Ex");
      Assert.Fail("Ha! Things dont just disappear!");
  }
  catch(ApplicationException ex){
      Debug.WriteLine("Caught Second");
  }
}
image Oh no! The test passed! I started to scream! The first exception was lost! It had to be there somewhere in the sand! I started digging...

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...