/// <summary>
        /// Creates a new <see cref="CatchBlockWrapper&lt;TParent&gt;" /> returning the block representing the catch block.
        /// </summary>
        /// <param name="parent">The parent to return to once the block has been ended.</param>
        /// <param name="type">The type of the exception being caught.</param>
        /// <param name="variableName">The variable name of the caught exception.</param>
        /// <param name="catchBody">The body of the catch statement to hand back to the calling body to improve the fluent syntax.</param>
        /// <returns>The new <see cref="CatchBlockWrapper&lt;TParent&gt;" /> representing the catch statement.</returns>
        /// <exception cref="ArgumentNullException">When the <paramref name="parent"/> or <paramref name="type"/> is null, the exception is thrown.</exception>
        /// <exception cref="ArgumentException">
        ///		<para>When <paramref name="type"/> does not derive from <see cref="Exception"/>, the exception is thrown.</para>
        ///		<para>- Or -</para>
        ///		<para>When the <paramref name="variableName"/> is non-null and empty or whitespace, the exception is thrown.</para>
        /// </exception>
        public static CatchBlockWrapper <TParent> Create(TParent parent, Type type, string variableName, out Block <TParent> catchBody)
        {
            Debug.Assert((parent != null), "The parent argument cannot be null.");
            Debug.Assert((type != null), "The type argument cannot be null.");
            Debug.Assert(typeof(Exception).IsAssignableFrom(type), "The type argument must derive from Exception.");

            ParameterExpression variable;

            if (variableName != null)
            {
                variableName = variableName.Trim();

                Debug.Assert((variableName.Length > 0), "The variableName argument cannot be empty or whitespace.");

                variable = Expression.Variable(type, variableName);
            }
            else
            {
                variable = null;
            }

            var catchBlockWrapper = new CatchBlockWrapper <TParent>(parent, type, variable);

            catchBody = catchBlockWrapper.catchBlock;

            return(catchBlockWrapper);
        }
Example #2
0
        /// <summary>
        /// Creates a catch block for the try block (no variable and catches all <typeparamref name="TException"/>s).
        /// </summary>
        /// <typeparam name="TException">The type of the <see cref="Exception"/> to catch.</typeparam>
        /// <returns>The catch's <see cref="Block&lt;TParent&gt;"/> to begin inserting statements.</returns>
        public Block <Try <TParent> > Catch <TException>() where TException : Exception
        {
            Block <Try <TParent> > catchBody;

            this.lstCatchBlocks.Add(CatchBlockWrapper <Try <TParent> > .Create(this, typeof(TException), null, out catchBody));

            return(catchBody);
        }
Example #3
0
        /// <summary>
        /// Creates a catch block for the try block (with a variable and catches all <typeparamref name="TException"/>s).
        /// </summary>
        /// <typeparam name="TException">The type of the <see cref="Exception"/> to catch.</typeparam>
        /// <param name="variableName">The variable name of the caught exception.</param>
        /// <returns>The catch's <see cref="Block&lt;TParent&gt;"/> to begin inserting statements.</returns>
        /// <exception cref="ArgumentException">
        ///		<para>When the <paramref name="variableName"/> is a name that is null, empty, or whitespace, the exception is thrown.</para>
        ///		<para>- Or -</para>
        ///		<para>When the <paramref name="variableName"/> is a name that is already declared, the exception is thrown.</para>
        ///	</exception>
        public Block <Try <TParent> > Catch <TException>(string variableName) where TException : Exception
        {
            Block <Try <TParent> > catchBody;

            if (string.IsNullOrWhiteSpace(variableName))
            {
                throw new ArgumentException("The variable name cannot be null, empty, or whitespace.", "variableName");
            }

            variableName = variableName.Trim();

            if (this.GetVariablesInScope().Any(x => x.Name == variableName))
            {
                throw new ArgumentException(string.Format("Variable, {0}, is already declared.  Please assign a different name.", variableName));
            }

            this.lstCatchBlocks.Add(CatchBlockWrapper <Try <TParent> > .Create(this, typeof(TException), variableName, out catchBody));

            return(catchBody);
        }