using System; // ElementaryExtensions API: using ElementaryExtensions; namespace MiscellaneousDemo { //************************************************************************************************** static partial class Program { //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /// /// Fatal Error and Unbreakable Region tests. /// static bool FatalErrorURTest() { int nDialogItemIdx= ЄConsole.NumberedListDialog ( "Fatal Error Simulation and Unbreakable Region Test:", new string[] { "Raise directly", "Unhandled exception", "Unhandled exception chain", "Unhandled exception chains (two concatenated chains)", "Implicit exception in unbreakable code region", "Safe throwing from UR (will not cause fatal error)" }, ЄConsoleDialogListNumeration.EndWithZero, "Cancel" ); if (nDialogItemIdx<0) return false; ЄConsole.Print(); switch (nDialogItemIdx) { case 0: ЄConsole.PrintOperation("Simulating fatal error (raising directly)"); ЄFatalError.Raise("Simulated error."); break; case 1: ЄConsole.PrintOperation("Simulating fatal error (throwing exception)"); throw new ApplicationException("Simulated error."); case 2: ЄConsole.PrintOperation("Simulating fatal error (throwing exception chain)"); try { throw new ApplicationException("Simulated error #1."); } catch (ApplicationException e) { throw new ApplicationException("Simulated error #2.",e); } case 3: ЄConsole.PrintOperation("Simulating fatal error (throwing two concatenated exception chains)"); // We need to wrap the throwing this way. (Else we will not see the both chains, // because unhandled exception handler will report the error prior to "finally" block // in "ThrowConcatenatedChains" method.) try { ThrowConcatenatedChains(); } catch { throw; } break; case 4: case 5: try { URExceptionTest(nDialogItemIdx==5); } catch (Exception e) { Sound.Play(SoundSignal.Error); ЄConsole.PrintError( ЄException.ComposeChainedMessage( e, ЄExceptionMessageChainingOptions.ConsoleStyle )); } break; default: throw new ЄDeadCodeBranchException(); } return true; } /// /// Throwing two concatenated exception chains. /// static void ThrowConcatenatedChains() { // Variable for saving of primary exception (chain): Exception exception=null; try { // Throwing primary exception chain: try { throw new ApplicationException("Simulated error #1."); } catch (ApplicationException e) { throw new ApplicationException("Simulated error #2.",e); } } catch (Exception e) { // Saving and rethrowing primary exception: exception=e; throw; } finally { // Throwing secondary exception chain (from finalization phase of the primary exception): try { // Throwing secondary exception chain: try { throw new InvalidOperationException("Simulated error #3."); } catch (InvalidOperationException e) { throw new InvalidOperationException("Simulated error #4.",e); } } catch (Exception e) { // Throwing concatenated chains (e+exception): throw ЄException.ConcatenatePreviousChain(e,exception); } } } /// /// Testing exception throwing from UR-region. /// static void URExceptionTest(bool bSafeThrowing) { ЄConsole.PrintOperation( bSafeThrowing ? "Performing safe exception throwing from unbreakable region" : "Performing unsafe exception throwing from unbreakable region" ); // Fully unbreakable region formed as extended user's callback: ЄUnbreakableRegion.ExecuteX ( delegate (ЄUnbreakableRegion ur) { // Unbreakable code block: if (bSafeThrowing) // Safe exception throwing (will not cause fatal error): throw ur.PrepareForSafeThrowing( new ApplicationException("Simulated error.") ); else // Unsafe exception throwing (will cause fatal error): throw new ApplicationException("Simulated error."); } ); } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ } // Program //************************************************************************************************** } // MiscellaneousDemo