A SpecialException is not an Exception, or is it?
-
Bernie was changing the design of some classes, and had to refactor some of them consequently. Also, some tests were broken due to his changes to the design. He had to look at those tests, and faced a new challenge to his knowledge of C# and the .Net world:
Exception expectedException = null; try { someItem.Initialize(); } catch (ConfigurationException ex) { expectedException = ex; } catch (Exception ex) { expectedException = ex; } if (expectedException == null) { Assert.Fail($"Expected ConfigurationException not thrown for '{keyMissing}'."); } else if (!(expectedException is ConfigurationException)) { Assert.Fail("Another exception than the expected ConfigurationException is thrown."); }
When someItem.Initialize throws a ConfigurationException, it will be caught by the first catch, and assigned to the local variable expectedException; in case of a different Exception, by the second catch. But why do we need two different catches here? Would the catch(Exception ex) not catch a ConfigurationException? Or would an Assert later on fail? Oh, by the way, why is that variable called expectedException when it is the actual exception?
Too many questions for such a shot code snippet.
catch { // indicate that there is a catch return 22; }
-
@berniethebernie said in A SpecialException is not an Exception, or is it?:
Would the catch(Exception ex) not catch a ConfigurationException?
Not if there's an earlier
catch
block that catches it, no. The first catch that matches a thrown exception is the one that handles it.Of course, there are plenty of other WTFs in this code. Anything that relies on an exception being thrown for expects flow is seriously screwed
-
This post is deleted!
-
@berniethebernie said in A SpecialException is not an Exception, or is it?:
catch { // indicate that there is a catch return 22; }
-
What the hell is supposed to happen in that code? I can't tell.
-
@cartman82 said in A SpecialException is not an Exception, or is it?:
What the hell is supposed to happen in that code? I can't tell.
Looks like a unit test that checks if a
ConfigurationException
is thrown. Or a very roundabout way to do:Assert.Throws<ConfigurationException>(() => someItem.Initialize());
-
@berniethebernie said in A SpecialException is not an Exception, or is it?:
catch { // indicate that there is a catch return 22; }
Could be worse. Could be C++:
throw 22;
-
@steve_the_cynic said in A SpecialException is not an Exception, or is it?:
Could be worse. Could be C++:
throw 22;
.NET allows you to do things like that via the
throw
MSIL instruction. Though it's probably a good thing C# doesn't support that :D
-
@raceprouk said in A SpecialException is not an Exception, or is it?:
@steve_the_cynic said in A SpecialException is not an Exception, or is it?:
Could be worse. Could be C++:
throw 22;
.NET allows you to do things like that via the
throw
MSIL instruction. Though it's probably a good thing C# doesn't support that :DThat's probably in MSIL because C++/CLR needs to support it. I don't see any other reason for it.
-
@steve_the_cynic J# needs it to be able to throw random Throwables, and I think Fujitsu requested it for some deep COBOL-related jitsu-fu.
-
@twelvebaud said in A SpecialException is not an Exception, or is it?:
Fujitsu
@twelvebaud said in A SpecialException is not an Exception, or is it?:
jitsu-fu
:Iseewhatyoudidthere.webm: