--- /dev/null
+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!!!");
+ }
+ }
+}
--- /dev/null
+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("")]
+
--- /dev/null
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
+ <ProductVersion>12.0.0</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{ED460508-5B0F-4E3F-8F37-77B092F350DD}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <RootNamespace>TryWithResourcesC</RootNamespace>
+ <AssemblyName>Try-With-Resources-C#</AssemblyName>
+ <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug</OutputPath>
+ <DefineConstants>DEBUG;</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <Externalconsole>true</Externalconsole>
+ <PlatformTarget>x86</PlatformTarget>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+ <DebugType>full</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release</OutputPath>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <Externalconsole>true</Externalconsole>
+ <PlatformTarget>x86</PlatformTarget>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Program.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+</Project>
\ No newline at end of file
--- /dev/null
+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;
+ }
+ }
+ }
+}
+