From f37d115981ae567dbc47354297dcaa3f52fe4926 Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Wed, 2 Jul 2014 09:53:13 +0200 Subject: [PATCH] using statement C# (try with resources Java) --- .../Try-With-Resources-C#/Program.cs | 107 +++++++++++++++++++++ .../Properties/AssemblyInfo.cs | 22 +++++ .../Try-With-Resources-C#.csproj | 42 ++++++++ .../Try-With-Resources-C#/under_the_scenes.txt | 84 ++++++++++++++++ 4 files changed, 255 insertions(+) create mode 100644 Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/Program.cs create mode 100644 Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/Properties/AssemblyInfo.cs create mode 100644 Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/Try-With-Resources-C#.csproj create mode 100644 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#/Program.cs b/Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/Program.cs new file mode 100644 index 0000000..109fe9b --- /dev/null +++ b/Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/Program.cs @@ -0,0 +1,107 @@ +using System; + +// +// SEE FILE: under_the_scenes.txt +// (it should be in the same directory as this file) +// +namespace TryWithResourcesC +{ + class MainClass + { + public static void Main(string[] args) + { + Console.WriteLine("BEGIN FIRST EXAMPLE"); + + using (ResourceFirst resourceOne = new ResourceFirst()) + using (ResourceSecond resourceTwo = new ResourceSecond()) + { + resourceTwo.DoSomething(); + resourceOne.DoSomething(); + } + + Console.WriteLine("END FIRST EXAMPLE"); + + + Console.WriteLine("BEGIN SECOND EXAMPLE"); + + using (ResourceFourth resourceFourth = new ResourceFourth()) + using (ResourceFifth resourceFifth = new ResourceFifth()) + { + resourceFifth.DoSomething(); + resourceFourth.DoSomething(); + } + + Console.WriteLine("END SECOND EXAMPLE"); + } + } + + + public class ResourceFirst : IDisposable + { + public void DoSomething() + { + Console.WriteLine("ResourceFirst: DoSomething"); + throw new Exception("ResourceFirst DoSomething Exception!!!"); + } + + public void Dispose() + { + Console.WriteLine("I am the Dispose of ResourceFirst"); + throw new Exception("ResourceFirst Dispose Exception!!!"); + } + } + + public class ResourceSecond : IDisposable + { + public void DoSomething() + { + Console.WriteLine("ResourceSecond: DoSomething"); + } + + public void Dispose() + { + Console.WriteLine("I am the Dispose of ResourceSecond"); + throw new Exception("ResourceSecond Dispose Exception!!!"); + } + } + + + public class ResourceFourth : IDisposable + { + public ResourceFourth() + { + throw new Exception("ResourceFourth Constructor Exception!!!"); + } + + public void DoSomething() + { + Console.WriteLine("ResourceFourth: DoSomething"); + throw new Exception("ResourceFourth DoSomething Exception!!!"); + } + + public void Dispose() + { + Console.WriteLine("I am the Dispose of ResourceFourth"); + throw new Exception("ResourceFourth Dispose Exception!!!"); + } + } + + public class ResourceFifth: IDisposable + { + public ResourceFifth() + { + throw new Exception("ResourceFifth Constructor Exception!!!"); + } + + public void DoSomething() + { + Console.WriteLine("ResourceFifth: DoSomething"); + } + + public void Dispose() + { + Console.WriteLine("I am the Dispose of ResourceFifth"); + throw new Exception("ResourceFifth Dispose Exception!!!"); + } + } +} diff --git a/Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/Properties/AssemblyInfo.cs b/Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..6d4f190 --- /dev/null +++ b/Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/Properties/AssemblyInfo.cs @@ -0,0 +1,22 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. +[assembly: AssemblyTitle("Try-With-Resources-C#")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("gumartinm.name")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("gumartinm.name")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. +[assembly: AssemblyVersion("1.0.*")] +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/Try-With-Resources-C#.csproj b/Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/Try-With-Resources-C#.csproj new file mode 100644 index 0000000..3dda028 --- /dev/null +++ b/Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/Try-With-Resources-C#.csproj @@ -0,0 +1,42 @@ + + + + Debug + x86 + 12.0.0 + 2.0 + {ED460508-5B0F-4E3F-8F37-77B092F350DD} + Exe + TryWithResourcesC + Try-With-Resources-C# + v4.5 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + true + x86 + + + full + true + bin\Release + prompt + 4 + true + x86 + + + + + + + + + + \ No newline at end of file diff --git a/Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/under_the_scenes.txt b/Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/under_the_scenes.txt new file mode 100644 index 0000000..0b77632 --- /dev/null +++ b/Allgemeines/Try-With-Resources-C#/Try-With-Resources-C#/under_the_scenes.txt @@ -0,0 +1,84 @@ +using (ResourceFirst resourceOne = new ResourceFirst()) +using (ResourceSecond resourceTwo = new ResourceSecond()) +{ + resourceTwo.DoSomething(); + resourceOne.DoSomething(); +} + + +using statement under the scenes: + +The UnhandledException event handles uncaught exceptions thrown from the main UI thread. +The ThreadException event handles uncaught exceptions thrown from non-UI threads. +Application.ThreadException; +AppDomain.CurrentDomain.UnhandledException; + +try { + // AppDomain + Program(); +} +catch(Exception e) +{ + AppDomain.CurrentDomain.UnhandledException = e; +} + +if (AppDomain.CurrentDomain.UnhandledException != null) +{ + Console.WriteLine("[ERROR] FATAL UNHANDLED EXCEPTION: {0}", e.ToString()); +} + + + +Program() +{ + ResourceFirst resourceOne = new ResourceFirst() + try{ + ResourceSecond resourceSecond = new ResourceSecond() + resourceOne.DoSomething(); + resourceSecond.DoSomehting(); + } + // When debuggin this catch exists. Does it exist when running without debug? + // I guess, it will not exist but not sure. + catch(Exception e) + { + Console.WriteLine("Unhandled Exception:"); + Console.WriteLine(e.ToString()); + throw e; + } + finally + { + if (resourceSecond != null) + { + try { + resourceSecond.Dispose(); + } + // When debuggin this catch exists. Does it exist when running without debug? + // I guess, it will not exist but not sure. + catch(Exception e) + { + Console.WriteLine("Unhandled Exception:"); + Console.WriteLine(e.ToString()); + throw e; + } + finally + { + resourceOne.Dispose(); + } + } + else + { + try { + resourceOne.Dispose(); + } + // When debuggin this catch exists. Does it exist when running without debug? + // I guess, it will not exist but not sure. + catch(Exception e) + { + Console.WriteLine("Unhandled Exception:"); + Console.WriteLine(e.ToString()); + throw e; + } + } + } +} + -- 2.1.4