using System;
using System.Windows.Forms ;

namespace StructuredExceptionHandlingComponentCS
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	public class Class1
	{
		public Class1()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		
		public void MiddleFunction(bool bThrowException)
		{
			
			MyOwnDisposableObject myObj = null ;

			// Entering this block causes a handler to be written onto the stack


			try
			{
				// Create an object we want to make sure to Dispose

				myObj = new MyOwnDisposableObject ( ) ;

				BottomFunction(bThrowException) ;
			}

				// The code in this Finally handler is executed whenever execution leaves the
				// Try block for any reason. We care most about the case in which BottomFunction
				// throws an exception and the stack is unwound. Without the Finally handler,
				// we'd have no chance to clean up from that exception.

			finally
			{
				myObj.Dispose ( ) ;
			}
		}
		
		public void BottomFunction(bool bThrowException) 
		{
			if (bThrowException)
			{
				// Create a new Exception object, setting its "Message" property
				// which can only be done in the constructor

				MyOwnException MyException = new MyOwnException ("Exception thrown by BottomFunction") ;

				// Set the new Exception's Source property, which can be done anywhere

				MyException.Source = "Understanding Microsoft.NET Chapter 2 ExceptionComponent" ;

				// Throw the exception

				throw MyException ;
			}
		}
	}
}
