<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx</link><description>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</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8942725</link><pubDate>Thu, 11 Sep 2008 05:03:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8942725</guid><dc:creator>Dean Harding</dc:creator><description>&lt;p&gt;About that TryParse thing, something that's bothered me is that while there's Int32.TryParse (etc), there's no Enum.TryParse. Is there any reason for that? Enum.Parse is something I do on occasion, and it's, ehem, vexing that I have to do it in a try/catch...&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8942777</link><pubDate>Thu, 11 Sep 2008 05:32:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8942777</guid><dc:creator>c</dc:creator><description>&lt;p&gt;The problem with the exogenous exceptions is that it's very hard to figure out the full list of ones you need to catch. &amp;nbsp;For example, try coming up with the full list of exceptions that could possibly be thrown when you open a file. &amp;nbsp;In addition to all the usual &amp;quot;file not found, directory not found, access denied, etc&amp;quot; messages you have to handle weirder cases, like the file being on a UNC share, the file being on an overlong junction, the file being pulled from a shadow copy, the file being served from a local directory that is actually a redirected folder, the file existing on a removable drive, the file living on a fake drive that is really a redirect to a URL, etc.&lt;/p&gt;
&lt;p&gt;Coming up with that full list of exceptions is really hard. &amp;nbsp;But if you miss one, your app crashes. &amp;nbsp;So, of course, you catch (Exception) and handle it with a generic &amp;quot;that didn't work&amp;quot; message. &amp;nbsp;But now you're committing the &amp;quot;sin&amp;quot; of &amp;quot;catch (Exception)&amp;quot;.&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8943699</link><pubDate>Thu, 11 Sep 2008 10:26:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8943699</guid><dc:creator>KooKiz</dc:creator><description>&lt;p&gt;The &amp;quot;do not catch fatal exceptions&amp;quot; need to be clarified a bit. I agree that you probably cannot do anything to recover from the error, but you can at least do some logging stuff. I guess you consider in this article that &amp;quot;catching an exception&amp;quot; and &amp;quot;Logging some stuff in a catch block&amp;quot; are two differents concepts.&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8943988</link><pubDate>Thu, 11 Sep 2008 11:52:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8943988</guid><dc:creator>Inigo</dc:creator><description>&lt;p&gt;Interesting classification. From a Java perspective, I'd say:&lt;/p&gt;
&lt;p&gt;Fatal exceptions are Errors (i.e. unchecked, and typically not caught) &lt;/p&gt;
&lt;p&gt;Boneheaded exceptions are typically RuntimeExceptions (i.e. unchecked)&lt;/p&gt;
&lt;p&gt;Exogenous exceptions are the reason for checked exceptions&lt;/p&gt;
&lt;p&gt;Vexing exceptions are the reason people complain about checked exceptions&lt;/p&gt;
&lt;p&gt;The relative frequencies of Exogenous vs Vexing exceptions in the language's libraries could determine whether checked exceptions are a good idea for that language or not.&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8944071</link><pubDate>Thu, 11 Sep 2008 12:14:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8944071</guid><dc:creator>Doug</dc:creator><description>&lt;p&gt;This is an excellent top-level taxonomy for exceptions. My biggest general complaint about the .NET framework is that the framework's exception hierarchy is completely useless. The hierarchy really should have been broken into something like what you describe (though Vexing and Exogenous can't truly be split via the type system). Instead of ApplicationException and SystemException, we should have had something like System.FatalException or System.DangerousException (parent class of StackOverflow, ExecutionEngine, AccessViolation, etc., the ones that should basically never be caught), System.BoneheadException or System.CodeErrorException (parent class of Argument, NullReference, or IndexOutOfBounds, etc., the ones that should immediately trigger a bug report), and System.RuntimeException (parent class of most other exceptions, the ones that might indicate a runtime failure but might also indicate a bug or a design flaw).&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8944079</link><pubDate>Thu, 11 Sep 2008 12:17:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8944079</guid><dc:creator>Matt</dc:creator><description>&lt;p&gt;XmlSerialization =&amp;gt; FormatExceptions left right and center.&lt;/p&gt;
&lt;p&gt;Defininitely in the Vexing section if you're trying to debug a specific formatting problem in it since lots of perfectly valid stuff is dealt with by catching the FormatExceptions.&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8944098</link><pubDate>Thu, 11 Sep 2008 12:20:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8944098</guid><dc:creator>[ICR]</dc:creator><description>&lt;p&gt;@C&lt;/p&gt;
&lt;p&gt;While it doesn't catch all of them, you can catch IOException to get a lot of them.&lt;/p&gt;
&lt;p&gt;I've always thought that while having the explicitness of Java's checked and unchecked exceptions (having to explicitly say what it throws) is bad, it would have been nice to have a distinction between fatal and non-fatal exceptions (a different base class). That way you can catch non-fatal exceptions for things like logging while letting fatal exceptions through.&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8944118</link><pubDate>Thu, 11 Sep 2008 12:27:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8944118</guid><dc:creator>Matt</dc:creator><description>&lt;p&gt;Sorry Doug I disagree. they type hierachy is a very blunt tool which shouldn't be used for this purpose.&lt;/p&gt;
&lt;p&gt;If you are catching an exception &amp;nbsp;you either want ***really*** specific. So your response can be well defined (FileNotFound, Autorization, specific IO exceptions mainly[1]).&lt;/p&gt;
&lt;p&gt;If you're catching anything else it is solely note the occurrence (optionally increasing the amount of information available) and rethrow or handle a domain transition (between app domains or to create your own specific exit code for a process) as such Catching Exception is fine&lt;/p&gt;
&lt;p&gt;Anything else is almost certainly a BadIdea&lt;/p&gt;
&lt;p&gt;[1] something the could definitely be improved, If I open a stream I'd like to know the difference between a file being locked verses a file being read only verses not being there etc...&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8944281</link><pubDate>Thu, 11 Sep 2008 13:22:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8944281</guid><dc:creator>Ollie Riches</dc:creator><description>&lt;p&gt;'Writing good error handling code is hard in any language'&lt;/p&gt;
&lt;p&gt;Absolutely! thats why I think more effort should have been put into the exception strategy for .Net. &lt;/p&gt;
&lt;p&gt;Yes the idea of 'checked exceptions' in java seems a good idea in theory but in practice it can get out of hand and I can understand why this wasn't implemented in .Net. I can also see there were ideas that all user (dev = me) exceptions should have been derived from system.application and therefore easy to handle but that idea broke down very quickly. I don't understand why the exceptions aren't grouped\segregated along the lines of namespaces - it would be nice to be able to catch just one exception type for all user related file exceptions - invalid path, invalid filename, permissions etc.&lt;/p&gt;
&lt;p&gt;I would love the ability to do this in a 'contract first approach' - be able to define on an interface the exception types that can be generated by the implementation, then at compile time this constraint could be checked and verified.&lt;/p&gt;
&lt;p&gt;Anyway great post cos I'm in the middle of trying to sort kinda thing out :) &lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8944525</link><pubDate>Thu, 11 Sep 2008 15:31:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8944525</guid><dc:creator>MiB</dc:creator><description>&lt;p&gt;2 Dean Harding&lt;/p&gt;
&lt;p&gt;You can use Enum.IsDefined for checking and then safely call Parse.&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8944710</link><pubDate>Thu, 11 Sep 2008 17:42:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8944710</guid><dc:creator>Randolpho</dc:creator><description>&lt;p&gt;Eric wrote this:&lt;/p&gt;
&lt;p&gt;&amp;quot;Just let your &amp;quot;finally&amp;quot; blocks run and hope for the best.&amp;quot;&lt;/p&gt;
&lt;p&gt;Keep in mind that there are times when a finally block will *not* run, and they're almost always during Fatal exceptions. &lt;/p&gt;
&lt;p&gt;What can we do about it? Don't put &amp;quot;hast to be run&amp;quot; code in your finally block -- only use finally for resource de-allocation. &lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8944740</link><pubDate>Thu, 11 Sep 2008 18:10:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8944740</guid><dc:creator>Michael Urman</dc:creator><description>&lt;p&gt;In the try/catch case, the catch covers all the processing that occurs within the using block. To avoid this you have to manually code a try/catch Dispose, as using cannot be attached to an already assigned variable. Both of these options smell bad. Is there a trick that can get you the best of both - limited scope on the FileNotFoundException, and the syntactic support of the using block?&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8944757</link><pubDate>Thu, 11 Sep 2008 18:26:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8944757</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;&amp;gt; as using cannot be attached to an already assigned variable&lt;/p&gt;
&lt;p&gt;Why do you say that?&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8944784</link><pubDate>Thu, 11 Sep 2008 18:55:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8944784</guid><dc:creator>Michael Urman</dc:creator><description>&lt;p&gt;Perhaps I'm mistaken? I just tested this in VS2008 and VS2005 and it works, so my memory is foggy. More likely I hit the 'Use of unassigned variable' error case if you don't have the throw or return in the sequence below.&lt;/p&gt;
&lt;p&gt;The sequence of Declare mydisposable / try { assign mydisposable } catch { throw or return } / using (mydisposable) { } is a little clunky, but not half as bad as without being able to use using that way. Thanks!&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8944816</link><pubDate>Thu, 11 Sep 2008 19:13:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8944816</guid><dc:creator>Tony Cox</dc:creator><description>&lt;p&gt;Is there another category of exceptions, which perhaps I would term &amp;quot;internal exceptions&amp;quot;? (Perhaps this is really just a subcategory of exogenous exceptions?)&lt;/p&gt;
&lt;p&gt;What I mean by &amp;quot;internal exceptions&amp;quot; is exceptions you throw and catch internally to a single component because doing so makes your program structurally more readable and maintainable, but that you do not (necessarily) intend to let fly out of your component to an external consumer.&lt;/p&gt;
&lt;p&gt;For example, suppose you have a component which parses some sort of fairly complex data file. Structurally, let's say it's convenient to write your parser in a recursive fashion, burrowing top-down through the file as it encounters elements to parse. However, errors of various kinds could be detected at any one of many levels of this parsing process, including all the way down at a leaf node. Let's say it's okay to stop trying to parse the rest of the file when you encounter the first error. How do you report these errors back up to the caller?&lt;/p&gt;
&lt;p&gt;You could use the traditional error code reporting method, bubbling back up a return code at each level of parsing, but that adds a lot of boilerplate error passing code to every level. It's also not terribly easy to elegantly attach additional information to the error. So instead, I think a perfectly reasonable pattern is to throw exceptions where errors occur, and then catch them all at the top-level method of the parser. You can then decide how to report this to the caller (maybe letting them handle the exception instead is okay, or maybe you want to use another method).&lt;/p&gt;
&lt;p&gt;It's an exogenous exception in that the root cause is something not under your control (the input file, which came from elsewhere), but you threw the exception yourself because it was a convenient way to structure your code.&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8944909</link><pubDate>Thu, 11 Sep 2008 20:09:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8944909</guid><dc:creator>Bryan Watts</dc:creator><description>&lt;p&gt;&amp;quot;...but you threw the exception yourself because it was a convenient way to structure your code.&amp;quot;&lt;/p&gt;
&lt;p&gt;Expection handling!&lt;/p&gt;
</description></item><item><title>Exception wisdom</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8944957</link><pubDate>Thu, 11 Sep 2008 20:26:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8944957</guid><dc:creator>Ramblings</dc:creator><description>&lt;p&gt;It's pretty rare that I do straight-up trackbacks into other people's blogs, but the latest post by Eric Lippert really deserves it. He discusses the four different classes of exceptions very eloquently. While he's talking about .NET, the same truths..&lt;/p&gt;
</description></item><item><title>Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8945776</link><pubDate>Fri, 12 Sep 2008 05:29:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8945776</guid><dc:creator>DotNetKicks.com</dc:creator><description>&lt;p&gt;You've been kicked (a good thing) - Trackback from DotNetKicks.com&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8946228</link><pubDate>Fri, 12 Sep 2008 14:19:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8946228</guid><dc:creator>swythan</dc:creator><description>&lt;p&gt;I attended a session by Jason Clark, who claimed to have worked on defining the exception strategy for the BCL (in v1.0?). He said the rule was:&lt;/p&gt;
&lt;p&gt;* The method name MUST be (contain?) a verb describing the effect of the function.&lt;/p&gt;
&lt;p&gt;* If that effect does not completely occur, the method MUST throw an exception.&lt;/p&gt;
&lt;p&gt;Thus double.Parse(string) throws an exception if it does not parse the string.&lt;/p&gt;
&lt;p&gt;Also:&lt;/p&gt;
&lt;p&gt;* If there are common causes of failure, ways SHOULD be provided to check if it will fail before calling the function (as far as possible).&lt;/p&gt;
&lt;p&gt;E.g. : Enum.IsDefined, File.Exists&lt;/p&gt;
&lt;p&gt;This isn't always provided, and can't always be bullet-proof (e.g. File.Exists, as described earlier) but helps avoid many common exceptions.&lt;/p&gt;
&lt;p&gt;IF there is a very common failure scenario where using exception handling causes a (measurable) performance degradation, then there's another set of rules:&lt;/p&gt;
&lt;p&gt;* Add another function that handles that error condition and JUST that error condition by returning e.g. false.&lt;/p&gt;
&lt;p&gt;* The new function MUST have a name that reflects the fact that it might not succeed.&lt;/p&gt;
&lt;p&gt;Thus: bool double.TryParse(string) which will, of course, throw exceptions if it hits any failure other than a badly formatted string (e.g. OutOfMemoryException).&lt;/p&gt;
&lt;p&gt;He also said that the reason that CLR exceptions are slow is that they are built on top of Win32 SEH. The CLR team apparently told him there was no reason they HAD to be built on top of SEH, it was just easier. They apparently said that if the speed of exceptions ever got into (e.g) the top 10 performance issues seen in real applications, they would probably re-implement them to be faster. The implication, of course, is that they're along way outside the top 10 right now! :-)&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8947121</link><pubDate>Fri, 12 Sep 2008 18:32:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8947121</guid><dc:creator>Philip Fitzsimons</dc:creator><description>&lt;p&gt;hmm, interesting idea - how would we implement this?&lt;/p&gt;
&lt;p&gt;public class LippertException: Exception, ISerializable;&lt;/p&gt;
&lt;p&gt;public class FatalLippertException: LippertException;&lt;/p&gt;
&lt;p&gt;public class BoneheadedLippertException: LippertException;&lt;/p&gt;
&lt;p&gt;public class VexingLippertException: LippertException; /* or fom bonehead? */&lt;/p&gt;
&lt;p&gt;public class ExogenousLippertException: VexingLippertException;&lt;/p&gt;
&lt;p&gt;maybe? ;-)&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8952963</link><pubDate>Mon, 15 Sep 2008 21:37:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8952963</guid><dc:creator>Michael Hohlios</dc:creator><description>&lt;p&gt;So this article sparked a bit of a controversy where I work.&lt;/p&gt;
&lt;p&gt;We all agree with your logic of Vexing Exceptions but we do have one area that none of us can agree on. &lt;/p&gt;
&lt;p&gt;When a user enters in the username and password does the login Manager throw and exception or does it offer a TryLogin?&lt;/p&gt;
&lt;p&gt;Some of us believe it should be an Exception because you expect the user to be logged in and it is exceptional if the users credentials are incorrect. &lt;/p&gt;
&lt;p&gt;While the rest of us think that you should always expect that the users input could be wrong making it not exceptional if they put in the wrong credentials. &lt;/p&gt;
&lt;p&gt;So really the argument comes down to which persons point of view for an exceptional case is programmed for. Is it the user or is it the developer? Users expect to be logged in thus the Exception handling on login, however, the Developer expects the user to be incorrect thus no exception.&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8953628</link><pubDate>Tue, 16 Sep 2008 11:11:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8953628</guid><dc:creator>Weeble</dc:creator><description>&lt;p&gt;I don't see what's vexing with double.Parse other than that exception handling is so slow. Part of the value of exceptions is that you can't accidentally forget to check the return code and muddle on thinking that - in this case - the string was correctly parsed. So *is* this just about performance, or are there deeper reasons to dislike exceptions? What exactly is &amp;quot;vexing&amp;quot; about a method throwing an exception when it is unable to do what you asked of it? Many people seem to agree that &amp;quot;exceptions should only be thrown in exceptional circumstances&amp;quot;, but everyone seems to understand something slightly different by &amp;quot;exceptional circumstances&amp;quot;. &lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8957572</link><pubDate>Thu, 18 Sep 2008 20:20:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8957572</guid><dc:creator>Nick Meyer</dc:creator><description>&lt;p&gt;@Michael&lt;/p&gt;
&lt;p&gt;Interesting question: to add my $.02, I don't think the user's should care how this is implemented. &amp;nbsp;All they need to know is that their credentials were incorrect, not how the login code treated the failure. &amp;nbsp;Moreover, it's not necessarily exceptional to have the wrong credentials, particularly if you've changed your password recently, like I have ;)&lt;/p&gt;
&lt;p&gt;I wouldn't say that the developer &amp;quot;expects&amp;quot; the user to be wrong, either. &amp;nbsp;I think the developer doesn't know what to expect. &amp;nbsp;Either case is as likely, so I would probably indicate the failure by a return value.&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8958653</link><pubDate>Fri, 19 Sep 2008 15:25:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8958653</guid><dc:creator>Mormegil</dc:creator><description>&lt;p&gt;I don’t like the concept of exogenous exceptions too much. There is nothing magical in exceptions in this situation – the OpenFile method (note that you did not use a constructor!) might as well return null (etc.) in case of an error instead of throwing an exception. The fact that you need to have atomic check/open has nothing to do with exceptions. So you could have categorized this as a vexing exception, too. The difference is just a bit of taste and syntactic sugar.&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8960966</link><pubDate>Mon, 22 Sep 2008 11:19:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8960966</guid><dc:creator>chris</dc:creator><description>&lt;p&gt;These are taught at university Java courses, in the format Inigo pointed out.&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8961455</link><pubDate>Mon, 22 Sep 2008 21:41:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8961455</guid><dc:creator>Jarrod Ramsey</dc:creator><description>&lt;p&gt;Good thoughts. &amp;nbsp;I liked your definition of exceptions and your categories. &amp;nbsp;Your pointing out the race condition was superb.&lt;/p&gt;
&lt;p&gt;One thing I disagree with is not trying to catch them. &amp;nbsp;I try to catch everything so I may log the errors. &amp;nbsp; Otherwise, the error may thrown and the program die, but the admin won't know what caused it. &amp;nbsp;Could bad logic have allowed the program to terminate willingly, even though it wasn't ready to end or did someone send a kill signal to it?&lt;/p&gt;
&lt;p&gt;And even though they can be a pain, .NET stack traces have the ability to provide some critcal information, so if you do nothing more than catch and log a general exception, you have set your self up to proactively understand what caused your program to cease.&lt;/p&gt;
&lt;p&gt;Secondly, you stated to not catch bonehead exceptions. &amp;nbsp;In my opinion, you never want the exception to be thrown up the stack until it displays the trace to the screen or spit out an unhandled exception error for the user to see. &amp;nbsp;Log it, email it, anything, but do something with it, then rethrow it or return a &amp;quot;polished&amp;quot; error message.&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8962334</link><pubDate>Tue, 23 Sep 2008 18:04:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8962334</guid><dc:creator>David Nelson</dc:creator><description>&lt;p&gt;@Jarrod&lt;/p&gt;
&lt;p&gt;I think you took Eric too literally. When he says &amp;quot;don't catch&amp;quot; these exceptions, he means don't catch them at the point where they occur in an attempt to soldier on despite the problem. Of course it is good practice to use a global error handler to log any &amp;quot;unhandled&amp;quot; exceptions that reach the start of the call stack, and display an appropriate error message to the user.&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8962346</link><pubDate>Tue, 23 Sep 2008 18:13:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8962346</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Sure. Use mechanisms (such as Watson) to backstop exceptions and report them to the user, or, even better, to the development team. &lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8964725</link><pubDate>Thu, 25 Sep 2008 11:25:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8964725</guid><dc:creator>madduck</dc:creator><description>&lt;p&gt;The problem is trickier than it seems. If the blah blah blah code itself throws a FileNotFoundException, do you still want to catch it? Probably not, since it broadens the catch beyond its intent. You're quite prepared for the file at hand to be missing or unreadable, but do you really want the same catch block to trigger when a configuration file is missing deep inside some library code? More likely than not, you want the latter exception to go straight to the debugger. Delegates or lambdas provide an interesting generalised solution for this:&lt;/p&gt;
&lt;p&gt;public static bool Exogenous&amp;lt;T, E&amp;gt;(Func&amp;lt;T&amp;gt; get, Action&amp;lt;T&amp;gt; use) where E : Exception&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;T value;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;try {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;value = get();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;} catch (E) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return false;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;use(value);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;return true;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Usage is quite simple, though some may need time to acclimatise:&lt;/p&gt;
&lt;p&gt;if (!Exogenous&amp;lt;File, FileNotFoundException&amp;gt;(&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; () =&amp;gt; OpenFile(filename, ForReading),&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; file =&amp;gt; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // blah blah blah&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }))&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;// Handle filename not found&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8966312</link><pubDate>Fri, 26 Sep 2008 13:49:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8966312</guid><dc:creator>Nick</dc:creator><description>&lt;p&gt;Interesting article. &amp;nbsp;One of the first things I do when I'm given unfamiliar code is search for &amp;quot;catch&amp;quot; to see what kind of developer(s) I'm dealing with.&lt;/p&gt;
&lt;p&gt;Nothing bothers me more than two patterns (which I see far too often):&lt;/p&gt;
&lt;p&gt;catch(Exception x)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; throw Exception(&amp;quot;Failed to achieve happieness&amp;quot;);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;and&lt;/p&gt;
&lt;p&gt;catch(Excetpion)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;// no code at all here&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;return false;&lt;/p&gt;
&lt;p&gt;So while you're on the subject of catching things. &amp;nbsp; Don't obliterate important debugging information in your catch block. &amp;nbsp;Don't leave *any* catch block completely empty. &amp;nbsp;If there's nothing to do then use&lt;/p&gt;
&lt;p&gt;catch(Exception x)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;// let the sap maintaining your code see what you're hiding.&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;if(Debugger.IsAttached)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Debug.WriteLine(x);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8969010</link><pubDate>Mon, 29 Sep 2008 17:32:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8969010</guid><dc:creator>Marc</dc:creator><description>&lt;p&gt;I wish there were a way of checking a string is a valud guid, without calling new Guid(s) and catching the exception. I check it's length is 36, not exactly water proof.&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8970119</link><pubDate>Tue, 30 Sep 2008 13:29:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8970119</guid><dc:creator>sam</dc:creator><description>&lt;p&gt;Use a regex to match the string, no exception catching required.&lt;/p&gt;
</description></item><item><title>Blog Carnival #6</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8970723</link><pubDate>Wed, 01 Oct 2008 00:12:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8970723</guid><dc:creator>IHateSpaghetti {code}</dc:creator><description>&lt;p&gt;In this carnival there&amp;amp;#39;re a lot of software design/patterns and frameworks a bit of SOA, UML, DSL&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#8972038</link><pubDate>Wed, 01 Oct 2008 21:44:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8972038</guid><dc:creator>Michael</dc:creator><description>&lt;p&gt;I really enjoyed this post and the attendant comments. &amp;nbsp;I you ever felt inclined to address the related topic of when to throw exceptions, I would be a quite happy reader as you address the topic so well.&lt;/p&gt;
&lt;p&gt;~Cheers~&lt;/p&gt;
</description></item><item><title>re: Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx#9336315</link><pubDate>Sat, 17 Jan 2009 21:44:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9336315</guid><dc:creator>temp.stuff</dc:creator><description>&lt;p&gt;I don't see the big difference between vexing and exogenous exceptions in the examples you made. Just like I could say that TryParse() can be implemented like this:&lt;/p&gt;
&lt;p&gt;TryParse() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;try {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.Parse();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;} catch (...) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return false;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;I can say that a &amp;quot;TryOpen()&amp;quot; method can be implemented as:&lt;/p&gt;
&lt;p&gt;TryOpen() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;try { this.Open(); }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;catch(...) (return false;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;and with this new method there's no race when doing:&lt;/p&gt;
&lt;p&gt;if (!FileExists(filename))&lt;/p&gt;
&lt;p&gt; &amp;nbsp;// Handle filename not found&lt;/p&gt;
&lt;p&gt;else&lt;/p&gt;
&lt;p&gt; &amp;nbsp;using ( File f = TryOpenFile(filename, ForReading) )&lt;/p&gt;
&lt;p&gt; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;// Blah blah blah&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
</description></item></channel></rss>