static void DefaultNoExceptionShielding() { Console.WriteLine("Getting salary for 'jsmith'..."); Console.WriteLine(); SalaryCalculator calc = new SalaryCalculator(); Console.WriteLine("Result is: {0}", calc.GetWeeklySalary("jsmith", 0)); }
static void DefaultNoExceptionShielding() { Console.WriteLine("Getting salary for 'jsmith'..."); Console.WriteLine(); SalaryCalculator calc = new SalaryCalculator(); Console.WriteLine("Result is: {0}", calc.GetWeeklySalary("jsmith", 0)); }
static void ProvidingAdminAssistance() { Console.WriteLine("Getting salary for 'jsmith'..."); Console.WriteLine(); // NOTE: Any exception raised when creating the SalaryCalculator // class instance will not be handled using this approach. SalaryCalculator calc = new SalaryCalculator(); decimal result = exManager.Process(() => calc.GetWeeklySalary("jsmith", 0), "AssistingAdministrators"); Console.WriteLine("Result is: {0}", result); }
static void LoggingTheException() { try { Console.WriteLine("Getting salary for 'jsmith'..."); Console.WriteLine(); // NOTE: Any exception raised when creating the SalaryCalculator // class instance will not be handled using this approach. var calc = new SalaryCalculator(); decimal result = exManager.Process(() => calc.GetWeeklySalary("jsmith", 0), "LoggingAndReplacingException"); Console.WriteLine("Result is: {0}", result); } catch (Exception ex) { MenuOption.ShowExceptionDetails(ex); Console.WriteLine("Open the Windows Application Event Log to see the logged exception details."); } }
static void WithWrapExceptionShieldingStatic() { Console.WriteLine("Getting salary for 'jsmith'..."); Console.WriteLine(); // NOTE: Any exception raised when creating the SalaryCalculator // class instance will not be handled using this approach. SalaryCalculator calc = new SalaryCalculator(); decimal result = 0; try { result = calc.GetWeeklySalary("jsmith", 0); } catch (Exception ex) { Exception exceptionToThrow; if (ExceptionPolicy.HandleException(ex, "ExceptionShielding", out exceptionToThrow)) { if (exceptionToThrow == null) { throw; } else { throw exceptionToThrow; } } } Console.WriteLine("Result is: {0}", result); // NOTE: If you do not need to return the value from the function, you can // simply consume it within the lambda expression. This is a simple example: // ------------------------------ //exManager.Process(() => // { // SalaryCalculator calc = new SalaryCalculator(); // Console.WriteLine("Result is: {0}", calc.GetWeeklySalary("jsmith", 0)); // }, // "ExceptionShielding"); // ------------------------------ // This approach also allows you to handle any exception raised by creating the // instance of the SalaryCalculator class. }
static void WithWrapExceptionShielding() { Console.WriteLine("Getting salary for 'jsmith'..."); Console.WriteLine(); // NOTE: Any exception raised when creating the SalaryCalculator // class instance will not be handled using this approach. SalaryCalculator calc = new SalaryCalculator(); var result = exManager.Process(() => calc.GetWeeklySalary("jsmith", 0), "ExceptionShielding"); Console.WriteLine("Result is: {0}", result); // NOTE: If you do not need to return the value from the function, you can // simply consume it within the lambda expression. This is a simple example: // ------------------------------ //exManager.Process(() => // { // SalaryCalculator calc = new SalaryCalculator(); // Console.WriteLine("Result is: {0}", calc.GetWeeklySalary("jsmith", 0)); // }, // "ExceptionShielding"); // ------------------------------ // This approach also allows you to handle any exception raised by creating the // instance of the SalaryCalculator class. }
static void ExecutingCodeAroundException() { //------------------------------------------------------ // Note that this is a somewhat contrived exeample designed to // show how you can use the HandleException method, detect different // exception types, and ignore specific types of exceptions. try { // Execute code that raises a DivideByZeroException. Console.WriteLine("Getting salary for 'jsmith' ... this will raise a DivideByZero exception."); SalaryCalculator calc = new SalaryCalculator(); Console.WriteLine("Result is: {0}", calc.RaiseDivideByZeroException("jsmith", 0)); } catch (Exception ex) { Exception newException; bool rethrow = exManager.HandleException(ex, "LogAndWrap", out newException); if (rethrow) { // Exception policy setting is "ThrowNewException". // Code here to perform any clean up tasks required. // Then throw the exception returned by the exception handling policy. throw newException; } else { // Exception policy setting is "None" so exception is not thrown. // Code here to perform any other processing required. // In this example, just ignore the exception and do nothing. Console.WriteLine("Detected and ignored Divide By Zero Error - no value returned."); } } try { // Now execute code that raises an ArgumentOutOfRangeException. // Use the same exception handling policy and catch section code. Console.WriteLine(); Console.WriteLine("Getting salary for 'jsmith' ... this will raise an ArgumentOutOfRange exception."); SalaryCalculator calc = new SalaryCalculator(); Console.WriteLine("Result is: {0}", calc.RaiseArgumentOutOfRangeException("jsmith", 0)); } catch (Exception ex) { Exception newException; bool rethrow = exManager.HandleException(ex, "LogAndWrap", out newException); if (rethrow) { // Exception policy could specify to throw the existing exception // or the new exception. // Code here to perform any clean up tasks required. if (newException == null) { throw; } else { throw newException; } } else { // Exception policy setting is "None". // Code here to perform any other processing required. // In this example, just ignore the exception and do nothing. Console.WriteLine("Detected and ignored Divide By Zero Error - no value returned."); } } }
static void ExecutingCodeAroundException() { //------------------------------------------------------ // Note that this is a somewhat contrived exeample designed to // show how you can use the HandleException method, detect different // exception types, and ignore specific types of exceptions. try { // Execute code that raises a DivideByZeroException. Console.WriteLine("Getting salary for 'jsmith' ... this will raise a DivideByZero exception."); SalaryCalculator calc = new SalaryCalculator(); Console.WriteLine("Result is: {0}", calc.RaiseDivideByZeroException("jsmith", 0)); } catch (Exception ex) { Exception newException; bool rethrow = exManager.HandleException(ex, "NotifyingRethrow", out newException); if (rethrow) { // Exception policy setting is "ThrowNewException". // Code here to perform any clean up tasks required. // Then throw the exception returned by the exception handling policy. throw newException; } else { // Exception policy setting is "None" so exception is not thrown. // Code here to perform any other processing required. // In this example, just ignore the exception and do nothing. Console.WriteLine("Detected and ignored Divide By Zero Error - no value returned."); } } try { // Now execute code that raises an ArgumentOutOfRangeException. // Use the same exception handling policy and catch section code. Console.WriteLine(); Console.WriteLine("Getting salary for 'jsmith' ... this will raise an ArgumentOutOfRange exception."); SalaryCalculator calc = new SalaryCalculator(); Console.WriteLine("Result is: {0}", calc.RaiseArgumentOutOfRangeException("jsmith", 0)); } catch (Exception ex) { Exception newException; bool rethrow = exManager.HandleException(ex, "NotifyingRethrow", out newException); if (rethrow) { // Exception policy setting is "ThrowNewException". // Code here to perform any clean up tasks required. // Then throw the exception returned by the exception handling policy. throw newException; } else { // Exception policy setting is "None". // Code here to perform any other processing required. // In this example, just ignore the exception and do nothing. Console.WriteLine("Detected and ignored Divide By Zero Error - no value returned."); } } }
static void WithWrapExceptionShielding() { Console.WriteLine("Getting salary for 'jsmith'..."); Console.WriteLine(); // NOTE: Any exception raised when creating the SalaryCalculator // class instance will not be handled using this approach. SalaryCalculator calc = new SalaryCalculator(); var result = exManager.Process(() => calc.GetWeeklySalary("jsmith", 0), "ExceptionShielding"); Console.WriteLine("Result is: {0}", result); // NOTE: If you do not need to return the value from the function, you can // simply consume it within the lambda expression. This is a simple example: // ------------------------------ //exManager.Process(() => // { // SalaryCalculator calc = new SalaryCalculator(); // Console.WriteLine("Result is: {0}", calc.GetWeeklySalary("jsmith", 0)); // }, // "ExceptionShielding"); // ------------------------------ // This approach also allows you to handle any exception raised by creating the // instance of the SalaryCalculator class. }
static void WithReplaceExceptionShielding() { Console.WriteLine("Getting salary for 'jsmith'..."); Console.WriteLine(); // NOTE: Any exception raised when creating the SalaryCalculator // class instance will not be handled using this approach. SalaryCalculator calc = new SalaryCalculator(); decimal result = exManager.Process(() => calc.GetWeeklySalary("jsmith", 0), "ReplacingException"); Console.WriteLine("Result is: {0}", result); }
static void LoggingTheException() { try { Console.WriteLine("Getting salary for 'jsmith'..."); Console.WriteLine(); // NOTE: Any exception raised when creating the SalaryCalculator // class instance will not be handled using this approach. var calc = new SalaryCalculator(); decimal result = exManager.Process(() => calc.GetWeeklySalary("jsmith", 0), "LoggingAndReplacingException"); Console.WriteLine("Result is: {0}", result); } catch (Exception ex) { MenuOption.ShowExceptionDetails(ex); Console.WriteLine("Open the Windows Application Event Log to see the logged exception details."); } }
static void WithWrapExceptionShieldingStatic() { Console.WriteLine("Getting salary for 'jsmith'..."); Console.WriteLine(); // NOTE: Any exception raised when creating the SalaryCalculator // class instance will not be handled using this approach. SalaryCalculator calc = new SalaryCalculator(); decimal result = 0; try { result = calc.GetWeeklySalary("jsmith", 0); } catch (Exception ex) { Exception exceptionToThrow; if (ExceptionPolicy.HandleException(ex, "ExceptionShielding", out exceptionToThrow)) { if (exceptionToThrow == null) throw; else throw exceptionToThrow; } } Console.WriteLine("Result is: {0}", result); // NOTE: If you do not need to return the value from the function, you can // simply consume it within the lambda expression. This is a simple example: // ------------------------------ //exManager.Process(() => // { // SalaryCalculator calc = new SalaryCalculator(); // Console.WriteLine("Result is: {0}", calc.GetWeeklySalary("jsmith", 0)); // }, // "ExceptionShielding"); // ------------------------------ // This approach also allows you to handle any exception raised by creating the // instance of the SalaryCalculator class. }