Derik Whittaker

Syndication

News


A new feature request for C# I would like is fall through exceptions

Now before you write off this post as being ‘just another silly request’ hear me out.

Right now in C# we can do the following

switch( someObject )
{
	case Option1:
	case Option2:
	case Option3:
		// do something
		break;
	case Option4:
		// do something
		break:
} 

Allowing fall through for a switch statement is a powerful technique.  It allows for multiple case statements in the same switch block to use the exact same logic, oh, and it also allows for code reduction.

Given the fact that we can do this with a switch statement why can we not do this with try-catch statements? 

As an example(s) (or a suggestion of syntax) why could we not have the following

try
{

} catch ( SomeException1 ) {
} catch ( SomeException2 ) {
} catch ( SomeException3 ) {
} catch ( SomeException4 se ) { 
	// do something
} catch ( SomeException5 se ) {
	// do something
}

OR

try
{

} catch ( SomeException1, SomeException2, SomeException3, SomeException4 se ) { 
	// do something
} catch ( SomeException5 se ) {
	// do something
}
 

I know, I know you think I have lost my mind.  But think about it.  There are times when you want to handle multiple exception types with the exact same logic.  However today when you do this you are forced to either create a helper method to handle the logic or repeat the logic over and over again.  If we had the ability to allow for fall through exceptions we could reduce duplicated code and reduce code noise.

Now before you start asking questions like … let me provide some quick Q & A

Q) If you do this you will never be able to know exactly what type of exception was thrown.

A) You are correct, but in this scenario do you really care about the exception type?  If you did I am going to guess you would break it out into individual catch block

 

Q) When would this situation actually be practical?

A) Consider this.  You are creating a connection to a 3rd party resource (web service, wcf, api, etc) and that connection can throw a few different exceptions.  In some cases if the expected exception is throw you want to log and maybe roll back a transaction or something.  In this case i do not want to re-throw (or just let bubble up) the exception, but at the same time I do not want to duplicate my code.

 

So, let me have it.  Tell me how dumb this is.

Tell next time,


Posted 01-20-2010 5:58 PM by Derik Whittaker

[Advertisement]

Comments

Mark Lennox wrote re: A new feature request for C# I would like is fall through exceptions
on 01-20-2010 7:21 PM

It would be very handy - of course you could have some sort of Exception switch within the catch, but it wouldn't be half so cool :)

Eber Irigoyen wrote re: A new feature request for C# I would like is fall through exceptions
on 01-20-2010 7:30 PM

I don't think I've ever needed anything like this, but just a clarification on your suggested syntax, the first one is already supported, you'd just eat the exceptions

Derik Whittaker wrote re: A new feature request for C# I would like is fall through exceptions
on 01-20-2010 7:52 PM

@Eber,

yes, but remember I do not want to eat them, i want to perform some action.

Bráulio wrote re: A new feature request for C# I would like is fall through exceptions
on 01-20-2010 8:33 PM

I guess there are situations where this would be handy. But this is somewhat already supported by catching a more general exception which is a superclass of the exceptions you need to catch. For example, in an API called FooAPI, it is common to have some general exception types, like FooException, from which all API's exceptions derive from.

Darren Kopp wrote re: A new feature request for C# I would like is fall through exceptions
on 01-20-2010 9:10 PM

No need.

1. Catch Exception

2. Look for matching handler based on exception type

3. Run handler. If none, rethrow

Any number of ways to do this. The advantage of catching specific exceptions is merely for intellisense support and to make sure you only handle exceptions that you should handle. But you can catch exception and do whatever you want and rethrow if you can't handle it as well.

What you propose would:

1.  Lose any intellisense support.

2. Require co-variance between exceptions (maybe?), leaving you at basically the same place as catching exception and determining the type there and handling it.

Leon Breedt wrote re: A new feature request for C# I would like is fall through exceptions
on 01-20-2010 10:13 PM

I believe you can do something like this in IL already, C# just has no syntax for it.

Funnily enough, VB does have syntax for it.

Essentially, you can have a callback determine whether or not to enter a catch block, in addition to the type specification.

So, my understanding (not having tried it), is that you'd use Exception as the catch argument, but have your callback determine whether you're going to look at it or not (just pass it the exception variable as first argument).

blogs.msdn.com/.../the-good-and-the-bad-of-exception-filters.aspx

Chris Missal wrote re: A new feature request for C# I would like is fall through exceptions
on 01-20-2010 10:27 PM

I'm trying to imagine a scenario where this could be abused. New features in C# (like optional parameters) are nice for some, but prone to abuse for others.

I'll have to let this stew in my brain a bit, but I'm more concerned with people abusing language features because I don't want to have to clean them up later. :)

Thomas Eyde wrote re: A new feature request for C# I would like is fall through exceptions
on 01-21-2010 3:27 AM

OT: The real flaw, imho, is that we have the switch statement at all. Now that we do, the next flaw is that this statement follows C++ idioms, not C#.

Munim .NET wrote re: A new feature request for C# I would like is fall through exceptions
on 01-21-2010 4:04 AM

Fall through is discouraged, which doesn't follow OO principles.

For you need, the same code you that resides in all the catches can be extracted to a method or to an exception helper class. More organized I guess!

Derik Whittaker wrote re: A new feature request for C# I would like is fall through exceptions
on 01-21-2010 6:17 AM

@Munim,

You are going to have to help me here.  How does fall through not follow OO?

Also, I did state that you could extract the logic into a common method to be called in each case.  However you still must put that method call in multiple places.

Michael Chandler wrote re: A new feature request for C# I would like is fall through exceptions
on 01-21-2010 7:21 AM

try  

{  

  statement;

} catch (Exception e) {  

  if (e is SomeException1 || e is SomeException2 || e is SomeException3 || e is SomeException4) {

   // do something  

  }

  else {

     throw;

  }

}

Good enough if you really need to do that I reckon. I don't feel like I've had to do it many times. I certainly don't think it's common enough to warrant being implemented as a language feature.

sboulay wrote re: A new feature request for C# I would like is fall through exceptions
on 01-21-2010 1:04 PM

Java 7 will have the multi catch clause. Neil Gafter who wrote the specification works for Microsoft  .. maybe he'll propose the same thing for .net.

Jim Raden wrote re: A new feature request for C# I would like is fall through exceptions
on 01-21-2010 2:10 PM

In regard to your theoretical objection #1, you *will* know what kind of exception it is, either through the "object is Type" syntax or through "ex.GetType()".

That being said, this new feature might be just syntactic sugar, compiling down to the syntax suggested by Michael Chandler's example.

There's certainly a precedent for this: there are several C# 3.0 features that are merely syntactic sugar, for example, automatic properties (if I'm not mistaken). Even the "lock" keyword is syntactic sugar for code involving a Monitor object.

Bill Sorensen wrote re: A new feature request for C# I would like is fall through exceptions
on 01-22-2010 1:03 PM

I like your second option. I've wanted to do exactly that. My scenario is I have a method that calls several libraries, each of which can throw one or more exceptions. For exceptions A, B, and C I want to catch these in the high-level code and display a dialog box. All other exceptions are fatal. Right now I have three catch blocks, but the body of each is identical.

Michael Chandler's solution is interesting as well.

viagra wrote re: A new feature request for C# I would like is fall through exceptions
on 02-08-2010 11:58 AM

eXPWAZC  <a href="http://dqdqed.com/ ">CxBhaWA</a>

viagra wrote re: A new feature request for C# I would like is fall through exceptions
on 02-08-2010 12:22 PM

kfBRgZaQ  <a href="http://huvwlz.com/ ">pFhOZT</a>

Tim Yen wrote re: A new feature request for C# I would like is fall through exceptions
on 02-16-2010 2:53 AM

I wouldn't do fall through.  I prefer option 2 which is multiple exceptions handled by the same code.

A good idea.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)