--- /dev/null
+
+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
public static void Main(string[] args)
{
Console.WriteLine("BEGIN FIRST EXAMPLE");
+ Console.Out.Flush();
using (ResourceFirst resourceOne = new ResourceFirst())
using (ResourceSecond resourceTwo = new ResourceSecond())
}
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())
}
Console.WriteLine("END SECOND EXAMPLE");
+ Console.Out.Flush();
}
}
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!!!");
}
}
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!!!");
}
}
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!!!");
}
}
public void Dispose()
{
Console.WriteLine("I am the Dispose of ResourceFifth");
+ Console.Out.Flush();
throw new Exception("ResourceFifth Dispose Exception!!!");
}
}
<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>
+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())
{
{
ResourceFirst resourceOne = new ResourceFirst()
try{
- ResourceSecond resourceSecond = new ResourceSecond()
+ ResourceSecond resourceSecond = new ResourceSecond()
resourceOne.DoSomething();
resourceSecond.DoSomehting();
}