Click here to Skip to main content
15,313,724 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: .Net Core 6 jumps the shark Pin
Jon McKee4-Dec-21 14:21
professionalJon McKee4-Dec-21 14:21 
GeneralRe: .Net Core 6 jumps the shark Pin
MSBassSinger4-Dec-21 15:00
professionalMSBassSinger4-Dec-21 15:00 
GeneralRe: .Net Core 6 jumps the shark Pin
Jon McKee4-Dec-21 16:12
professionalJon McKee4-Dec-21 16:12 
GeneralRe: .Net Core 6 jumps the shark Pin
Richard Deeming5-Dec-21 22:00
mveRichard Deeming5-Dec-21 22:00 
GeneralRe: .Net Core 6 jumps the shark Pin
MSBassSinger7-Dec-21 8:53
professionalMSBassSinger7-Dec-21 8:53 
GeneralRe: .Net Core 6 jumps the shark Pin
Richard Deeming7-Dec-21 22:10
mveRichard Deeming7-Dec-21 22:10 
GeneralRe: .Net Core 6 jumps the shark Pin
MSBassSinger8-Dec-21 5:17
professionalMSBassSinger8-Dec-21 5:17 
GeneralRe: .Net Core 6 jumps the shark Pin
Richard Deeming8-Dec-21 6:18
mveRichard Deeming8-Dec-21 6:18 
MSBassSinger wrote:
To be more precise, constructor exceptions occur outside the MSIL exception handling. In order to catch the constructor exception, the "using" has to be wrapped in a try-catch of its own.
OK, that makes more sense now. Smile | :)

However, unless you specifically need different catch blocks for exceptions thrown from constructing the class and exceptions thrown from using the class, you can still get away with a single try..catch block in your code; it just needs to wrap the entire using block.
C#
[TestMethod]
public void UsingStatementTest()
{
    try
    {
        using (DisposableClass test = new())
        {
            String result = test.WhoIsIt();
        }
    }
    catch (Exception exOuter)
    {
        Assert.Fail($"ONLY: {exOuter.Message}");
    }
}

You'll still have the try..finally from the using block nested within the try..catch block from your own code. But as you can see from the MSIL of your own StandardUsageTest method, a try..catch..finally block is implemented as a try..catch block wrapped in a try..finally block.

The IL, for comparison:
MSIL
  1  .method public hidebysig static 
  2      void StandardUsageTest () cil managed 
  3  {
  4      // Method begins at RVA 0x2104
  5      // Code size 70 (0x46)
  6      .maxstack 3
  7      .locals init (
  8          [0] class DisposableClass test,
  9          [1] string result,
 10          [2] class [System.Private.CoreLib]System.Exception exOuter
 11      )
 12  
 13      IL_0000: nop
 14      IL_0001: ldnull
 15      IL_0002: stloc.0
 16      .try
 17      {
 18          .try
 19          {
 20              IL_0003: nop
 21              IL_0004: newobj instance void DisposableClass::.ctor()
 22              IL_0009: stloc.0
 23              IL_000a: ldloc.0
 24              IL_000b: callvirt instance string DisposableClass::WhoIsIt()
 25              IL_0010: stloc.1
 26              IL_0011: nop
 27              IL_0012: leave.s IL_0034
 28          } // end .try
 29          catch [System.Private.CoreLib]System.Exception
 30          {
 31              IL_0014: stloc.2
 32              IL_0015: nop
 33              IL_0016: call class [System.Private.CoreLib]System.IO.TextWriter [System.Console]System.Console::get_Error()
 34              IL_001b: ldstr "ONLY: "
 35              IL_0020: ldloc.2
 36              IL_0021: callvirt instance string [System.Private.CoreLib]System.Exception::get_Message()
 37              IL_0026: call string [System.Private.CoreLib]System.String::Concat(string, string)
 38              IL_002b: callvirt instance void [System.Private.CoreLib]System.IO.TextWriter::WriteLine(string)
 39              IL_0030: nop
 40              IL_0031: nop
 41              IL_0032: leave.s IL_0034
 42          } // end handler
 43  
 44          // sequence point: hidden
 45          IL_0034: leave.s IL_0045
 46      } // end .try
 47      finally
 48      {
 49          IL_0036: nop
 50          IL_0037: ldloc.0
 51          IL_0038: brtrue.s IL_003c
 52  
 53          IL_003a: br.s IL_0043
 54  
 55          IL_003c: ldloc.0
 56          IL_003d: call instance void DisposableClass::Dispose()
 57          IL_0042: nop
 58  
 59          IL_0043: nop
 60          IL_0044: endfinally
 61      } // end handler
 62  
 63      IL_0045: ret
 64  } // end of method Program::StandardUsageTest
MSIL
  1  .method public hidebysig static 
  2      void UsingStatementTest () cil managed 
  3  {
  4      // Method begins at RVA 0x2174
  5      // Code size 66 (0x42)
  6      .maxstack 3
  7      .locals init (
  8          [0] class DisposableClass test,
  9          [1] string result,
 10          [2] class [System.Private.CoreLib]System.Exception exOuter
 11      )
 12  
 13      IL_0000: nop
 14      .try
 15      {
 16          IL_0001: nop
 17          IL_0002: newobj instance void DisposableClass::.ctor()
 18          IL_0007: stloc.0
 19          .try
 20          {
 21              IL_0008: nop
 22              IL_0009: ldloc.0
 23              IL_000a: callvirt instance string DisposableClass::WhoIsIt()
 24              IL_000f: stloc.1
 25              IL_0010: nop
 26              IL_0011: leave.s IL_001e
 27          } // end .try
 28          finally
 29          {
 30              // sequence point: hidden
 31              IL_0013: ldloc.0
 32              IL_0014: brfalse.s IL_001d
 33  
 34              IL_0016: ldloc.0
 35              IL_0017: callvirt instance void [System.Private.CoreLib]System.IDisposable::Dispose()
 36              IL_001c: nop
 37  
 38              // sequence point: hidden
 39              IL_001d: endfinally
 40          } // end handler
 41  
 42          IL_001e: nop
 43          IL_001f: leave.s IL_0041
 44      } // end .try
 45      catch [System.Private.CoreLib]System.Exception
 46      {
 47          IL_0021: stloc.2
 48          IL_0022: nop
 49          IL_0023: call class [System.Private.CoreLib]System.IO.TextWriter [System.Console]System.Console::get_Error()
 50          IL_0028: ldstr "ONLY: "
 51          IL_002d: ldloc.2
 52          IL_002e: callvirt instance string [System.Private.CoreLib]System.Exception::get_Message()
 53          IL_0033: call string [System.Private.CoreLib]System.String::Concat(string, string)
 54          IL_0038: callvirt instance void [System.Private.CoreLib]System.IO.TextWriter::WriteLine(string)
 55          IL_003d: nop
 56          IL_003e: nop
 57          IL_003f: leave.s IL_0041
 58      } // end handler
 59  
 60      IL_0041: ret
 61  } // end of method Program::UsingStatementTest
SharpLab[^]

Practically the same number of lines for both. Smile | :)

The one advantage I can see to your method is that, assuming the constructor hasn't thrown an exception, the instance will be available and not disposed-of in your catch block. With the using block wrapped in a try..catch block, the instance will already have been disposed of, and the variable will be out of scope, by the time you reach the catch block.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: .Net Core 6 jumps the shark Pin
PIEBALDconsult11-Dec-21 8:50
professionalPIEBALDconsult11-Dec-21 8:50 
GeneralRe: .Net Core 6 jumps the shark Pin
zezba900029-Nov-21 9:35
Memberzezba900029-Nov-21 9:35 
GeneralRe: .Net Core 6 jumps the shark Pin
PIEBALDconsult11-Dec-21 8:42
professionalPIEBALDconsult11-Dec-21 8:42 
GeneralWhat the heck are they teaching philosophers in Florida? Pin
honey the codewitch28-Nov-21 5:54
mvahoney the codewitch28-Nov-21 5:54 
GeneralRe: What the heck are they teaching philosophers in Florida? Pin
trønderen28-Nov-21 6:04
Membertrønderen28-Nov-21 6:04 
GeneralRe: What the heck are they teaching philosophers in Florida? Pin
honey the codewitch28-Nov-21 6:05
mvahoney the codewitch28-Nov-21 6:05 
GeneralRe: What the heck are they teaching philosophers in Florida? Pin
honey the codewitch28-Nov-21 6:08
mvahoney the codewitch28-Nov-21 6:08 
GeneralRe: What the heck are they teaching philosophers in Florida? Pin
Daniel Pfeffer28-Nov-21 21:37
professionalDaniel Pfeffer28-Nov-21 21:37 
GeneralRe: What the heck are they teaching philosophers in Florida? Pin
Greg Utas28-Nov-21 6:06
mveGreg Utas28-Nov-21 6:06 
GeneralRe: What the heck are they teaching philosophers in Florida? Pin
honey the codewitch28-Nov-21 6:06
mvahoney the codewitch28-Nov-21 6:06 
GeneralRe: What the heck are they teaching philosophers in Florida? Pin
Greg Utas28-Nov-21 6:49
mveGreg Utas28-Nov-21 6:49 
GeneralRe: What the heck are they teaching philosophers in Florida? Pin
honey the codewitch28-Nov-21 6:50
mvahoney the codewitch28-Nov-21 6:50 
GeneralRe: What the heck are they teaching philosophers in Florida? Pin
Greg Utas28-Nov-21 6:53
mveGreg Utas28-Nov-21 6:53 
GeneralRe: What the heck are they teaching philosophers in Florida? Pin
Marc Clifton28-Nov-21 7:01
mvaMarc Clifton28-Nov-21 7:01 
GeneralIt is called a Doctorate of Philosophy because... Pin
Michael Breeden29-Nov-21 1:04
MemberMichael Breeden29-Nov-21 1:04 
GeneralRe: What the heck are they teaching philosophers in Florida? Pin
obermd29-Nov-21 6:39
Memberobermd29-Nov-21 6:39 
GeneralRe: What the heck are they teaching philosophers in Florida? Pin
Chris Maunder29-Nov-21 19:16
cofounderChris Maunder29-Nov-21 19:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.