示例#1
0
        /// <summary>
        /// Attempts to unwrap a caught exception and retrieve the thrown object if it should be
        /// handled by AS3 code.
        /// </summary>
        /// <param name="exception">The caught exception.</param>
        /// <param name="thrownValue">An output parameter where the object thrown will be written,
        /// if the exception should be handled by compiled ActionScript code.</param>
        /// <returns>True if the exception should be handled, false otherwise.</returns>
        /// <remarks>
        /// Calls to this method are inserted into code generated by the IL compiler; this method
        /// should not be used in .NET code.
        /// </remarks>
        public static bool tryUnwrapCaughtException(Exception exception, out ASAny thrownValue)
        {
            thrownValue = default;

            // Unwrap inner exceptions from TypeInitializationException and TargetInvocationException.
            while (exception is TypeInitializationException || exception is TargetInvocationException)
            {
                exception = exception.InnerException;
            }

            if (exception is AVM2Exception avm2Exception)
            {
                thrownValue = avm2Exception.thrownValue;
                return(true);
            }

            if (exception is NullReferenceException)
            {
                thrownValue = ErrorHelper.createErrorObject(ErrorCode.NULL_REFERENCE_ERROR);
                return(true);
            }

            return(false);
        }
示例#2
0
 /// <summary>
 /// Creates an <see cref="AVM2Exception"/> object wrapping a new instance of the Error class
 /// or a subclass of it with the given message and code.
 /// </summary>
 ///
 /// <param name="typeHandle">The <see cref="RuntimeTypeHandle"/> of the type of the error
 /// to create. This type must derive from <see cref="ASError"/> and have a public constructor
 /// with the signature <c>TError(string message, int code)</c>.</param>
 /// <param name="message">The value to set to the message property of the created
 /// <see cref="ASError"/> instance.</param>
 /// <param name="code">The value to set to the code property of the created
 /// <see cref="ASError"/> instance.</param>
 ///
 /// <returns>An <see cref="AVM2Exception"/> object wrapping the created instance of the
 /// given error type.</returns>
 public static AVM2Exception create(RuntimeTypeHandle typeHandle, string message, int code) =>
 new AVM2Exception(ErrorHelper.createErrorObject(Type.GetTypeFromHandle(typeHandle), code, message));