InfoQ Homepage News C# Futures: Deferred Error Handling

# C# Futures: Deferred Error Handling

This item in japanese

When writing robust software, there is often a need to perform a series of retriable operations. In order to make the system robust, each operation in the series can be coded so it is independent of the status of the previous operation. As a concrete example, consider a file processing pipeline.

The first step in the pipeline may be to poll a file server and download any newly detected files. The next step would be to parse those files and convert them into a more usable format. A third step would be to import the converted files into a database for further processing.

If the first step fails after downloading some of the files, the application as a whole shouldn’t abort. Rather, it should go onto the next step and start parsing the files that were successfully downloaded. This is acceptable because the missed files can simply be picked up during the next cycle.

To implement this pattern in C# one usually has to use a series of try-catch blocks.

try
{
}
catch (Exception ex)
{
//log errors
}
try
{
ParseAndConvertFiles();
}
catch (Exception ex)
{
//log errors
}
try
{
ImportFiles();
}
catch (Exception ex)
{
//log errors
}


With the Deferred Error Handling proposal, much of this boilerplate code can be removed.

#exception mode deferred
ParseAndConvertFiles();
ImportFiles();
if (Exception.LastException != null)
{
//log errors
Exception.ClearLastException();
}
#exception mode structured


In order to use deferred error handling, a new compiler directive called “exception mode” is used. This switches the current function between structured exception handling and the new deferred mode.

When using the deferred mode, the Exception.LastException property can be used to determine if an error has occurred. This stores only the most recent error, so if multiple errors occurred, all but the last will be lost.

This has caused some concern, as it would mean one should check LastException after each line, which would be contrary to the goal of reducing the amount of code needed.

To address this, an amendment to the proposal is to replace LastException with a stack is under consideration. This would allow the developer to see all of the exceptions that were thrown in reverse chronological order.

Another option being considered is the ability to specify a jump target.

#exception mode deferred error_handler
[...]
return;

error_handler:
//log error
#exception resume next


If the “next” modifier is omitted, the application will retry the failed statement rather than continuing onto the next line.

The use of both structured and deferred error handling in the same function can be problematic from a compiler standpoint. Deferred mode fundamentally changes the way the code is compiled, much like how C# implements closures and async/await without CLR support. In order to simplify the changes needed for the compiler, another syntax is being considered. This would use the same type of syntax we see with unsafe and checked blocks.

deferred
{
ParseAndConvertFiles();
ImportFiles();
}


Due to the magnitude of the change, this proposal is unlikely to be adopted before C# 9.

Style

## Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

• ##### Stop. Just stop. Seriously.

by Cameron Purdy /

• ##### Re: Stop. Just stop. Seriously.

by Jonathan Allen /

• ##### Re: Stop. Just stop. Seriously.

by Cameron Purdy /

• ##### Insert indignant subject here...

by Ron Hoyt /

• ##### Very good

by William Smith /

• ##### Terrifying

by Scott Hannen /

• ##### not a joke

by Dzmitry Lahoda /

• ##### Stop. Just stop. Seriously.

Your message is awaiting moderation. Thank you for participating in the discussion.

Why are you turning a relatively good looking language into a pile of s**t?

The "#" stuff is butt-ugly. (At least the proposed "deferred" keyword pretends to be something like C#.)

• ##### Re: Stop. Just stop. Seriously.

Your message is awaiting moderation. Thank you for participating in the discussion.

Check the date my good sir. Though the fact that you believe our little joke even for a moment does say something about how crazy fast C# is changing.

• ##### Re: Stop. Just stop. Seriously.

Your message is awaiting moderation. Thank you for participating in the discussion.

Well played, although you should delete these last two messages so that you can ensnare another idiot ;-)

• ##### Insert indignant subject here...

by Ron Hoyt /

Your message is awaiting moderation. Thank you for participating in the discussion.

And equally indignant message here, with sample VB3 code inserted as such:
On Error Resume Next
DangerousOperationThatCouldCauseErrors
If Err Then
WScript.StdErr.WriteLine "error " & Err.Number
WScript.Quit 1
End If
On Error GoTo 0

BTW, you had me hook line and sinker too... :)

• ##### Very good

Your message is awaiting moderation. Thank you for participating in the discussion.

I'm not normally a fan of April Fools jokes, but I enjoyed this one. C# being what it is (it is to Java what C++ is to C) I was quite a long way into the post before i realised it was a wind-up. Very funny!

• ##### Terrifying

by Scott Hannen /

Your message is awaiting moderation. Thank you for participating in the discussion.

My first reaction was, no, you can't be serious. "On Error Resume Next" wasn't an advanced feature that C# simply couldn't keep up with until now. It was what people used when they didn't want to have any idea what their code was doing or if it even worked. It's like driving to work with your eyes closed because if you didn't see anybody get run over it didn't happen.

Very well done. Thank you.

• ##### not a joke

Your message is awaiting moderation. Thank you for participating in the discussion.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.