using statement, try-with-resources C# style
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Thu, 3 Jul 2014 05:09:48 +0000 (07:09 +0200)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Thu, 3 Jul 2014 05:09:48 +0000 (07:09 +0200)
Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#.sln [new file with mode: 0644]
Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/Program.cs
Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/Try-With-Resources-C#.csproj
Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/under_the_scenes.txt

diff --git a/Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#.sln b/Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#.sln
new file mode 100644 (file)
index 0000000..0ae99d7
--- /dev/null
@@ -0,0 +1,20 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 2012
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Try-With-Resources-C#", "Try-With-Resources-C#\Try-With-Resources-C#.csproj", "{ED460508-5B0F-4E3F-8F37-77B092F350DD}"
+EndProject
+Global
+       GlobalSection(SolutionConfigurationPlatforms) = preSolution
+               Debug|x86 = Debug|x86
+               Release|x86 = Release|x86
+       EndGlobalSection
+       GlobalSection(ProjectConfigurationPlatforms) = postSolution
+               {ED460508-5B0F-4E3F-8F37-77B092F350DD}.Debug|x86.ActiveCfg = Debug|x86
+               {ED460508-5B0F-4E3F-8F37-77B092F350DD}.Debug|x86.Build.0 = Debug|x86
+               {ED460508-5B0F-4E3F-8F37-77B092F350DD}.Release|x86.ActiveCfg = Release|x86
+               {ED460508-5B0F-4E3F-8F37-77B092F350DD}.Release|x86.Build.0 = Release|x86
+       EndGlobalSection
+       GlobalSection(MonoDevelopProperties) = preSolution
+               StartupItem = Try-With-Resources-C#\Try-With-Resources-C#.csproj
+       EndGlobalSection
+EndGlobal
index 109fe9b..7335e2c 100644 (file)
@@ -11,6 +11,7 @@ namespace TryWithResourcesC
         public static void Main(string[] args)
         {
             Console.WriteLine("BEGIN FIRST EXAMPLE");
+            Console.Out.Flush();
 
             using (ResourceFirst resourceOne = new ResourceFirst())
             using (ResourceSecond resourceTwo = new ResourceSecond())
@@ -20,9 +21,11 @@ namespace TryWithResourcesC
             }
 
             Console.WriteLine("END FIRST EXAMPLE");
+            Console.Out.Flush();
 
 
             Console.WriteLine("BEGIN SECOND EXAMPLE");
+            Console.Out.Flush();
 
             using (ResourceFourth resourceFourth = new ResourceFourth())
             using (ResourceFifth resourceFifth = new ResourceFifth())
@@ -32,6 +35,7 @@ namespace TryWithResourcesC
             }
 
             Console.WriteLine("END SECOND EXAMPLE");
+            Console.Out.Flush();
         }
     }
 
@@ -41,12 +45,14 @@ namespace TryWithResourcesC
         public void DoSomething()
         {
             Console.WriteLine("ResourceFirst: DoSomething");
+            Console.Out.Flush();
             throw new Exception("ResourceFirst DoSomething Exception!!!");
         }
 
         public void Dispose()
         {
             Console.WriteLine("I am the Dispose of ResourceFirst");
+            Console.Out.Flush();
             throw new Exception("ResourceFirst Dispose Exception!!!");
         }
     }
@@ -56,11 +62,13 @@ namespace TryWithResourcesC
         public void DoSomething()
         {
             Console.WriteLine("ResourceSecond: DoSomething");
+            Console.Out.Flush();
         }
 
         public void Dispose()
         {
             Console.WriteLine("I am the Dispose of ResourceSecond");
+            Console.Out.Flush();
             throw new Exception("ResourceSecond Dispose Exception!!!");
         }
     }
@@ -76,12 +84,14 @@ namespace TryWithResourcesC
         public void DoSomething()
         {
             Console.WriteLine("ResourceFourth: DoSomething");
+            Console.Out.Flush();
             throw new Exception("ResourceFourth DoSomething Exception!!!");
         }
 
         public void Dispose()
         {
             Console.WriteLine("I am the Dispose of ResourceFourth");
+            Console.Out.Flush();
             throw new Exception("ResourceFourth Dispose Exception!!!");
         }
     }
@@ -101,6 +111,7 @@ namespace TryWithResourcesC
         public void Dispose()
         {
             Console.WriteLine("I am the Dispose of ResourceFifth");
+            Console.Out.Flush();
             throw new Exception("ResourceFifth Dispose Exception!!!");
         }
     }
index 3dda028..207c239 100644 (file)
@@ -19,8 +19,8 @@
     <DefineConstants>DEBUG;</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
-    <Externalconsole>true</Externalconsole>
     <PlatformTarget>x86</PlatformTarget>
+    <ConsolePause>false</ConsolePause>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
     <DebugType>full</DebugType>
index 57199f3..16dab07 100644 (file)
@@ -1,3 +1,39 @@
+Program.cs, output from example:
+
+BEGIN FIRST EXAMPLE
+ResourceSecond: DoSomething
+ResourceFirst: DoSomething
+
+Unhandled Exception:
+System.Exception: ResourceFirst DoSomething Exception!!!
+  at TryWithResourcesC.ResourceFirst.DoSomething () [0x0001a] in /home/gustavo/github/CSharpForFun/Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/Program.cs:49
+  at TryWithResourcesC.MainClass.Main (System.String[] args) [0x00029] in /home/gustavo/github/CSharpForFun/Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/Program.cs:20
+I am the Dispose of ResourceSecond
+
+Unhandled Exception:
+System.Exception: ResourceSecond Dispose Exception!!!
+  at TryWithResourcesC.ResourceSecond.Dispose () [0x0001a] in /home/gustavo/github/CSharpForFun/Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/Program.cs:72
+  at TryWithResourcesC.MainClass.Main (System.String[] args) [0x0003b] in /home/gustavo/github/CSharpForFun/Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/Program.cs:17
+I am the Dispose of ResourceFirst
+
+Unhandled Exception:
+System.Exception: ResourceFirst Dispose Exception!!!
+  at TryWithResourcesC.ResourceFirst.Dispose () [0x0001a] in /home/gustavo/github/CSharpForFun/Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/Program.cs:56
+  at TryWithResourcesC.MainClass.Main (System.String[] args) [0x0004d] in /home/gustavo/github/CSharpForFun/Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/Program.cs:16
+[ERROR] FATAL UNHANDLED EXCEPTION: System.Exception: ResourceFirst Dispose Exception!!!
+  at TryWithResourcesC.ResourceFirst.Dispose () [0x0001a] in /home/gustavo/github/CSharpForFun/Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/Program.cs:56
+  at TryWithResourcesC.MainClass.Main (System.String[] args) [0x0004d] in /home/gustavo/github/CSharpForFun/Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/Program.cs:16
+
+
+
+So, it is worse than Java because you can not see easily the hidden exceptions.
+As in Java it keeps running the Dispose methods even if some of them throw exception!!!
+
+SO, THE BEST WAY TO CLOSE DEVICES IN C# IS USING THE using statement!!!!!
+OTHERWISE YOU ARE GOING TO WRITE LOADS OF CODE IF YOU WANT TO DO THE SAME!!!!
+
+
+
 using (ResourceFirst resourceOne = new ResourceFirst())
 using (ResourceSecond resourceTwo = new ResourceSecond())
 {
@@ -33,7 +69,7 @@ Program()
 {
        ResourceFirst resourceOne = new ResourceFirst()
        try{
-               ResourceSecond resourceSecond = new ResourceSecond()    
+               ResourceSecond resourceSecond = new ResourceSecond()
                resourceOne.DoSomething();
                resourceSecond.DoSomehting();
        }