Exemplo n.º 1
0
        private static void Anonimowa(object sender, ContractFailedEventArgs e)
        {
            string messgae = e.FailureKind + ": " + e.Message;

            Console.WriteLine(messgae);
            e.SetUnwind();
        }
Exemplo n.º 2
0
        static partial void RaiseContractFailedEventImplementation(ContractFailureKind failureKind, String userMessage, String conditionText, Exception innerException, ref string resultFailureMessage)
        {
            if (failureKind < ContractFailureKind.Precondition || failureKind > ContractFailureKind.Assume)
            {
                throw new ArgumentException(SR.Format(SR.Arg_EnumIllegalVal, failureKind), nameof(failureKind));
            }
            Contract.EndContractBlock();

            string returnValue;
            String displayMessage             = "contract failed."; // Incomplete, but in case of OOM during resource lookup...
            ContractFailedEventArgs eventArgs = null;               // In case of OOM.

#if FEATURE_RELIABILITY_CONTRACTS
            System.Runtime.CompilerServices.RuntimeHelpers.PrepareConstrainedRegions();
#endif
            try
            {
                displayMessage = GetDisplayMessage(failureKind, userMessage, conditionText);
                EventHandler <ContractFailedEventArgs> contractFailedEventLocal = contractFailedEvent;
                if (contractFailedEventLocal != null)
                {
                    eventArgs = new ContractFailedEventArgs(failureKind, displayMessage, conditionText, innerException);
                    foreach (EventHandler <ContractFailedEventArgs> handler in contractFailedEventLocal.GetInvocationList())
                    {
                        try
                        {
                            handler(null, eventArgs);
                        }
                        catch (Exception e)
                        {
                            eventArgs.thrownDuringHandler = e;
                            eventArgs.SetUnwind();
                        }
                    }
                    if (eventArgs.Unwind)
                    {
                        // unwind
                        if (innerException == null)
                        {
                            innerException = eventArgs.thrownDuringHandler;
                        }
                        throw new ContractException(failureKind, displayMessage, userMessage, conditionText, innerException);
                    }
                }
            }
            finally
            {
                if (eventArgs != null && eventArgs.Handled)
                {
                    returnValue = null; // handled
                }
                else
                {
                    returnValue = displayMessage;
                }
            }
            resultFailureMessage = returnValue;
        }
Exemplo n.º 3
0
        private static void RaiseContractFailedEventImplementation(ContractFailureKind failureKind, string userMessage, string conditionText, Exception innerException, ref string resultFailureMessage)
        {
            string str2;

            if ((failureKind < ContractFailureKind.Precondition) || (failureKind > ContractFailureKind.Assume))
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", new object[] { failureKind }), "failureKind");
            }
            string message            = "contract failed.";
            ContractFailedEventArgs e = null;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                message = GetDisplayMessage(failureKind, userMessage, conditionText);
                if (contractFailedEvent != null)
                {
                    e = new ContractFailedEventArgs(failureKind, message, conditionText, innerException);
                    foreach (EventHandler <ContractFailedEventArgs> handler in contractFailedEvent.GetInvocationList())
                    {
                        try
                        {
                            handler(null, e);
                        }
                        catch (Exception exception)
                        {
                            e.thrownDuringHandler = exception;
                            e.SetUnwind();
                        }
                    }
                    if (e.Unwind)
                    {
                        if (Environment.IsCLRHosted)
                        {
                            TriggerCodeContractEscalationPolicy(failureKind, message, conditionText, innerException);
                        }
                        if (innerException == null)
                        {
                            innerException = e.thrownDuringHandler;
                        }
                        throw new ContractException(failureKind, message, userMessage, conditionText, innerException);
                    }
                }
            }
            finally
            {
                if ((e != null) && e.Handled)
                {
                    str2 = null;
                }
                else
                {
                    str2 = message;
                }
            }
            resultFailureMessage = str2;
        }
Exemplo n.º 4
0
        public static string?RaiseContractFailedEvent(ContractFailureKind failureKind, string?userMessage, string?conditionText, Exception?innerException)
        {
            if (failureKind < ContractFailureKind.Precondition || failureKind > ContractFailureKind.Assume)
            {
                throw new ArgumentException(SR.Format(SR.Arg_EnumIllegalVal, failureKind), nameof(failureKind));
            }

            string?returnValue;
            string displayMessage             = "contract failed."; // Incomplete, but in case of OOM during resource lookup...
            ContractFailedEventArgs?eventArgs = null;               // In case of OOM.

            try
            {
                displayMessage = GetDisplayMessage(failureKind, userMessage, conditionText);
                EventHandler <ContractFailedEventArgs> contractFailedEventLocal = InternalContractFailed;
                if (contractFailedEventLocal != null)
                {
                    eventArgs = new ContractFailedEventArgs(failureKind, displayMessage, conditionText, innerException);
                    foreach (EventHandler <ContractFailedEventArgs> handler in contractFailedEventLocal.GetInvocationList())
                    {
                        try
                        {
                            handler(null, eventArgs);
                        }
                        catch (Exception e)
                        {
                            eventArgs.thrownDuringHandler = e;
                            eventArgs.SetUnwind();
                        }
                    }
                    if (eventArgs.Unwind)
                    {
                        // unwind
                        if (innerException == null)
                        {
                            innerException = eventArgs.thrownDuringHandler;
                        }
                        throw new ContractException(failureKind, displayMessage, userMessage, conditionText, innerException);
                    }
                }
            }
            finally
            {
                if (eventArgs != null && eventArgs.Handled)
                {
                    returnValue = null; // handled
                }
                else
                {
                    returnValue = displayMessage;
                }
            }

            return(returnValue);
        }
Exemplo n.º 5
0
        private static void RaiseContractFailedEventImplementation(ContractFailureKind failureKind, string userMessage, string conditionText, Exception innerException, ref string resultFailureMessage)
        {
            if (failureKind < ContractFailureKind.Precondition || failureKind > ContractFailureKind.Assume)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (object)failureKind), "failureKind");
            }
            string str1 = "contract failed.";
            ContractFailedEventArgs e = (ContractFailedEventArgs)null;

            RuntimeHelpers.PrepareConstrainedRegions();
            string str2;

            try
            {
                str1 = ContractHelper.GetDisplayMessage(failureKind, userMessage, conditionText);
                EventHandler <ContractFailedEventArgs> eventHandler = ContractHelper.contractFailedEvent;
                if (eventHandler != null)
                {
                    e = new ContractFailedEventArgs(failureKind, str1, conditionText, innerException);
                    foreach (EventHandler <ContractFailedEventArgs> invocation in eventHandler.GetInvocationList())
                    {
                        try
                        {
                            invocation((object)null, e);
                        }
                        catch (Exception ex)
                        {
                            e.thrownDuringHandler = ex;
                            e.SetUnwind();
                        }
                    }
                    if (e.Unwind)
                    {
                        if (Environment.IsCLRHosted)
                        {
                            ContractHelper.TriggerCodeContractEscalationPolicy(failureKind, str1, conditionText, innerException);
                        }
                        if (innerException == null)
                        {
                            innerException = e.thrownDuringHandler;
                        }
                        throw new ContractException(failureKind, str1, userMessage, conditionText, innerException);
                    }
                }
            }
            finally
            {
                str2 = e == null || !e.Handled ? str1 : (string)null;
            }
            resultFailureMessage = str2;
        }
Exemplo n.º 6
0
		public static string RaiseContractFailedEvent (ContractFailureKind failureKind, string userMessage, string conditionText, Exception innerException)
		{

			StringBuilder msg = new StringBuilder (60);
			switch (failureKind) {
			case ContractFailureKind.Assert:
				msg.Append ("Assertion failed");
				break;
			case ContractFailureKind.Assume:
				msg.Append ("Assumption failed");
				break;
			case ContractFailureKind.Invariant:
				msg.Append ("Invariant failed");
				break;
			case ContractFailureKind.Postcondition:
				msg.Append ("Postcondition failed");
				break;
			case ContractFailureKind.PostconditionOnException:
				msg.Append ("Postcondition failed after throwing an exception");
				break;
			case ContractFailureKind.Precondition:
				msg.Append ("Precondition failed");
				break;
			default:
				throw new NotSupportedException ("Not supported: " + failureKind);
			}
			if (conditionText != null) {
				msg.Append (": ");
				msg.Append (conditionText);
			} else {
				msg.Append ('.');
			}
			if (userMessage != null) {
				msg.Append ("  ");
				msg.Append (userMessage);
			}
			string msgString = msg.ToString ();

			Exception handlerException = null;
			bool unwind = false, handled = false;

			var contractFailed = Contract.InternalContractFailedEvent;
			if (contractFailed != null) {
				// Execute all event handlers
				var handlers = contractFailed.GetInvocationList ();
				var e = new ContractFailedEventArgs (failureKind, msgString, conditionText, innerException);
				foreach (var handler in handlers) {
					try {
						handler.DynamicInvoke (null, e);
					} catch (Exception ex) {
						e.SetUnwind ();
						// If multiple handlers throw an exception then the specification states that it
						// is undetermined which one becomes the InnerException.
						handlerException = ex.InnerException;
					}
				}
				unwind = e.Unwind;
				handled = e.Handled;
			}

			if (unwind) {
				Exception ex = innerException ?? handlerException;
				throw new ContractException (msgString, failureKind, conditionText, userMessage, ex);
			}

			return handled ? null : msgString;
		}
Exemplo n.º 7
0
 private static void contractFailedEvent(object sender, ContractFailedEventArgs e)
 {
     e.SetUnwind();
     throw new Exception(e.Message);
 }
Exemplo n.º 8
0
        public static string RaiseContractFailedEvent(ContractFailureKind failureKind, string userMessage, string conditionText, Exception innerException)
        {
            StringBuilder msg = new StringBuilder(60);

            switch (failureKind)
            {
            case ContractFailureKind.Assert:
                msg.Append("Assertion failed");
                break;

            case ContractFailureKind.Assume:
                msg.Append("Assumption failed");
                break;

            case ContractFailureKind.Invariant:
                msg.Append("Invariant failed");
                break;

            case ContractFailureKind.Postcondition:
                msg.Append("Postcondition failed");
                break;

            case ContractFailureKind.PostconditionOnException:
                msg.Append("Postcondition failed after throwing an exception");
                break;

            case ContractFailureKind.Precondition:
                msg.Append("Precondition failed");
                break;

            default:
                throw new NotSupportedException("Not supported: " + failureKind);
            }
            if (conditionText != null)
            {
                msg.Append(": ");
                msg.Append(conditionText);
            }
            else
            {
                msg.Append('.');
            }
            if (userMessage != null)
            {
                msg.Append("  ");
                msg.Append(userMessage);
            }
            string msgString = msg.ToString();

            Exception handlerException = null;
            bool      unwind = false, handled = false;

            var contractFailed = Contract.InternalContractFailedEvent;

            if (contractFailed != null)
            {
                // Execute all event handlers
                var handlers = contractFailed.GetInvocationList();
                var e        = new ContractFailedEventArgs(failureKind, msgString, conditionText, innerException);
                foreach (var handler in handlers)
                {
                    try {
                        handler.DynamicInvoke(null, e);
                    } catch (Exception ex) {
                        e.SetUnwind();
                        // If multiple handlers throw an exception then the specification states that it
                        // is undetermined which one becomes the InnerException.
                        handlerException = ex.InnerException;
                    }
                }
                unwind  = e.Unwind;
                handled = e.Handled;
            }

            if (unwind)
            {
                Exception ex = innerException ?? handlerException;
                throw new ContractException(msgString, failureKind, conditionText, userMessage, ex);
            }

            return(handled ? null : msgString);
        }
Exemplo n.º 9
0
        static void RaiseContractFailedEventImplementation(ContractFailureKind failureKind, string userMessage,
                                                           string conditionText, Exception innerException, ref string resultFailureMessage)
        {
            if (failureKind < ContractFailureKind.Precondition || failureKind > ContractFailureKind.Assume)
            {
                throw new ArgumentException(string.Format("Invalid enum value: {0}", failureKind), "failureKind");
            }
            Contract.EndContractBlock();

            string returnValue;
            var    displayMessage             = "contract failed."; // Incomplete, but in case of OOM during resource lookup...
            ContractFailedEventArgs eventArgs = null;               // In case of OOM.

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                displayMessage = GetDisplayMessage(failureKind, userMessage, conditionText);
                var contractFailedEventLocal = _contractFailedEvent;
                if (contractFailedEventLocal != null)
                {
                    eventArgs = new ContractFailedEventArgs(failureKind, displayMessage, conditionText, innerException);
                    foreach (var @delegate in contractFailedEventLocal.GetInvocationList())
                    {
                        var handler = (EventHandler <ContractFailedEventArgs>)@delegate;
                        try
                        {
                            handler(null, eventArgs);
                        }
                        catch (Exception e)
                        {
#if NET35
                            eventArgs.ThrownDuringHandler = e;
#else
                            GC.KeepAlive(e);
#endif
                            eventArgs.SetUnwind();
                        }
                    }
                    if (eventArgs.Unwind)
                    {
#if NET35
                        // unwind
                        if (innerException == null)
                        {
                            innerException = eventArgs.ThrownDuringHandler;
                        }
#endif
                        throw new ContractException(failureKind, displayMessage, userMessage, conditionText, innerException);
                    }
                }
            }
            finally
            {
                if (eventArgs != null && eventArgs.Handled)
                {
                    returnValue = null; // handled
                }
                else
                {
                    returnValue = displayMessage;
                }
            }
            resultFailureMessage = returnValue;
        }
Exemplo n.º 10
0
        public static string RaiseContractFailedEvent(ContractFailureKind failureKind, string userMessage, string conditionText, Exception innerException)
        {
            if (failureKind < ContractFailureKind.Precondition || failureKind > ContractFailureKind.Assume)
            {
                throw new ArgumentException(SR.Format(SR.Arg_EnumIllegalVal, failureKind), nameof(failureKind));
            }
            Contract.EndContractBlock();

            string returnValue;
            string displayMessage = "contract failed.";  // Incomplete, but in case of OOM during resource lookup...

#if !FEATURE_CORECLR
            ContractFailedEventArgs eventArgs = null;  // In case of OOM.
#endif // !FEATURE_CORECLR
#if FEATURE_RELIABILITY_CONTRACTS
            System.Runtime.CompilerServices.RuntimeHelpers.PrepareConstrainedRegions();
#endif
            try
            {
                displayMessage = GetDisplayMessage(failureKind, userMessage, conditionText);
#if !FEATURE_CORECLR
                if (s_contractFailedEvent != null)
                {
                    eventArgs = new ContractFailedEventArgs(failureKind, displayMessage, conditionText, innerException);
                    foreach (EventHandler <ContractFailedEventArgs> handler in s_contractFailedEvent.GetInvocationList())
                    {
                        try
                        {
                            handler(null, eventArgs);
                        }
                        catch (Exception e)
                        {
                            eventArgs.thrownDuringHandler = e;
                            eventArgs.SetUnwind();
                        }
                    }
                    if (eventArgs.Unwind)
                    {
                        //if (Environment.IsCLRHosted)
                        //    TriggerCodeContractEscalationPolicy(failureKind, displayMessage, conditionText, innerException);

                        // unwind
                        if (innerException == null)
                        {
                            innerException = eventArgs.thrownDuringHandler;
                        }
                        throw new ContractException(failureKind, displayMessage, userMessage, conditionText, innerException);
                    }
                }
#endif // !FEATURE_CORECLR
            }
            finally
            {
#if !FEATURE_CORECLR
                if (eventArgs != null && eventArgs.Handled)
                {
                    returnValue = null; // handled
                }
                else
#endif // !FEATURE_CORECLR
                {
                    returnValue = displayMessage;
                }
            }
            return(returnValue);
        }
        private static void RaiseContractFailedEventImplementation(ContractFailureKind failureKind, string userMessage, string conditionText, Exception innerException, ref string resultFailureMessage)
        {
            if (failureKind < ContractFailureKind.Precondition || failureKind > ContractFailureKind.Assume)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", new object[]
                {
                    failureKind
                }), "failureKind");
            }
            string text = "contract failed.";
            ContractFailedEventArgs contractFailedEventArgs = null;

            RuntimeHelpers.PrepareConstrainedRegions();
            string text2;

            try
            {
                text = ContractHelper.GetDisplayMessage(failureKind, userMessage, conditionText);
                EventHandler <ContractFailedEventArgs> eventHandler = ContractHelper.contractFailedEvent;
                if (eventHandler != null)
                {
                    contractFailedEventArgs = new ContractFailedEventArgs(failureKind, text, conditionText, innerException);
                    foreach (EventHandler <ContractFailedEventArgs> eventHandler2 in eventHandler.GetInvocationList())
                    {
                        try
                        {
                            eventHandler2(null, contractFailedEventArgs);
                        }
                        catch (Exception thrownDuringHandler)
                        {
                            contractFailedEventArgs.thrownDuringHandler = thrownDuringHandler;
                            contractFailedEventArgs.SetUnwind();
                        }
                    }
                    if (contractFailedEventArgs.Unwind)
                    {
                        if (Environment.IsCLRHosted)
                        {
                            ContractHelper.TriggerCodeContractEscalationPolicy(failureKind, text, conditionText, innerException);
                        }
                        if (innerException == null)
                        {
                            innerException = contractFailedEventArgs.thrownDuringHandler;
                        }
                        throw new ContractException(failureKind, text, userMessage, conditionText, innerException);
                    }
                }
            }
            finally
            {
                if (contractFailedEventArgs != null && contractFailedEventArgs.Handled)
                {
                    text2 = null;
                }
                else
                {
                    text2 = text;
                }
            }
            resultFailureMessage = text2;
        }
Exemplo n.º 12
0
        static partial void RaiseContractFailedEventImplementation(ContractFailureKind failureKind, String userMessage, String conditionText, Exception innerException, ref string resultFailureMessage) 
        { 
            if (failureKind < ContractFailureKind.Precondition || failureKind > ContractFailureKind.Assume)
                throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", failureKind), "failureKind"); 
            Contract.EndContractBlock();

            String displayMessage = "contract failed.";  // Incomplete, but in case of OOM during resource lookup...
            ContractFailedEventArgs eventArgs = null;  // In case of OOM. 
            string returnValue;
#if FEATURE_RELIABILITY_CONTRACTS 
            System.Runtime.CompilerServices.RuntimeHelpers.PrepareConstrainedRegions(); 
#endif
            try 
            {
                displayMessage = GetDisplayMessage(failureKind, userMessage, conditionText);
                if (contractFailedEvent != null)
                { 
                    eventArgs = new ContractFailedEventArgs(failureKind, displayMessage, conditionText, innerException);
                    foreach (EventHandler<ContractFailedEventArgs> handler in contractFailedEvent.GetInvocationList()) 
                    { 
                        try
                        { 
                            handler(null, eventArgs);
                        }
                        catch (Exception e)
                        { 
                            eventArgs.thrownDuringHandler = e;
                            eventArgs.SetUnwind(); 
                        } 
                    }
                    if (eventArgs.Unwind) 
                    {
                        if (Environment.IsCLRHosted)
                            TriggerCodeContractEscalationPolicy(failureKind, displayMessage, conditionText, innerException);
 
                        // unwind
                        if (innerException == null) { innerException = eventArgs.thrownDuringHandler; } 
                        throw new ContractException(failureKind, displayMessage, userMessage, conditionText, innerException); 
                    }
                } 
            }
            finally
            {
                if (eventArgs != null && eventArgs.Handled) 
                {
                    returnValue = null; // handled 
                } 
                else
                { 
                    returnValue = displayMessage;
                }
            }
            resultFailureMessage = returnValue; 
        }