Never Say Never, Part One

Can you find a lambda expression that can be implicitly converted to Func<T> for any possible T? . . . . . . . . . . . Hint: The same lambda is convertible to Action as well. . . . . . . . . . Func<int> function = () => { throw new…


Asynchrony in C# 5, Part Eight: More Exceptions

(In this post I’ll be talking about exogenous, vexing, boneheaded and fatal exceptions. See this post for a definition of those terms.) If your process experiences an unhandled exception then clearly something bad and unanticipated has happened. If its a fatal exception then you’re already in no position to save the process; it is going…


Asynchrony in C# 5, Part Seven: Exceptions

Resuming where we left off (ha ha ha!) after that brief interruption: exception handling in “resumable” methods like our coroutine-like asynchronous methods is more than a little bit weird. To get a sense of how weird it is, you might want to first refresh your memory of my recent series on the design of iterator…


Too much reuse

A recent user question: I have code that maintains a queue of pending work items waiting to be completed on various different worker threads. In certain unfortunate fatal error situations I complete each of these by throwing an exception. Can I create just one exception object? Are there any issues throwing the same exception object…


As Timeless As Infinity

User: Recently I found out about a peculiar behaviour concerning division by zero in floating point numbers in C#. It does not throw an exception, as with integer division, but rather returns an “infinity”. Why is that? Eric: As I’ve often said, “why” questions are difficult for me to answer. My first attempt at an answer to…


Iterator Blocks, Part Five: Push vs Pull

A while back I posted some commentary I did for the Scripting Summer Games where I noted that there is an isomorphism between “pull” sequences and “push” events. Normally you think of events as something that “calls you”, pushing the event arguments at you. And normally you think of a sequence as something that you…


Iterator blocks Part Four: Why no yield in catch?

Now that you know why we disallow yielding in a finally, it’s pretty straightforward to see why we also disallow yielding in a catch. First off, we still have the problem that it is illegal to “goto” into the middle of the handler of a try-protected region. The only way to enter a catch block…


Iterator Blocks, Part Three: Why no yield in finally?

There are three scenarios in which code could be executing in a finally in an iterator block. In none of them is it a good idea to yield a value from inside the finally, so this is illegal across the board. The three scenarios are (1) normal cleanup, (2) exception cleanup, and (3) iterator disposal….


Vexing exceptions

Writing good error handling code is hard in any language, whether you have exception handling or not. When I’m thinking about what exception handling I need to implement in a given program, I first classify every exception I might catch into one of four buckets which I label fatal, boneheaded, vexing and exogenous. Fatal exceptions…