New tests.
[mono.git] / mcs / class / corlib / System / Exception.cs
index 2a451d9b5624db0c099b92948ac5faddd66b1675..b4a2f1ee665cf1c2faf13efd5797fdaf1548e158 100644 (file)
@@ -40,33 +40,40 @@ using System.Security.Permissions;
 namespace System
 {
        [Serializable]
-       [ClassInterface (ClassInterfaceType.AutoDual)]
-       public class Exception : ISerializable 
-#if NET_2_0
-       , _Exception
-#endif
+       [ComVisible(true)]
+       [ComDefaultInterface (typeof (_Exception))]
+       [ClassInterface (ClassInterfaceType.None)]
+       public class Exception : ISerializable, _Exception
        {
+#pragma warning disable 169, 649
+               #region Sync with object-internals.h
+               /* Stores the IPs and the generic sharing infos
+                  (vtable/MRGCTX) of the frames. */
                IntPtr [] trace_ips;
                Exception inner_exception;
-               string message;
+               internal string message;
                string help_link;
                string class_name;
                string stack_trace;
-               string remote_stack_trace;
+               // formerly known as remote_stack_trace (see #425512):
+               string _remoteStackTraceString;
                int remote_stack_index;
-               int hresult = unchecked ((int)0x80004005);
+               internal int hresult = -2146233088;
                string source;
+               IDictionary _data;
+               #endregion
+#pragma warning restore 169, 649               
 
                public Exception ()
                {
                }
 
-               public Exception (string msg)
+               public Exception (string message)
                {
-                       message = msg;
+                       this.message = message;
                }
 
-               protected Exception (SerializationInfo info, StreamingContext sc)
+               protected Exception (SerializationInfo info, StreamingContext context)
                {
                        if (info == null)
                                throw new ArgumentNullException ("info");
@@ -75,17 +82,23 @@ namespace System
                        message             = info.GetString ("Message");
                        help_link           = info.GetString ("HelpURL");
                        stack_trace         = info.GetString ("StackTraceString");
-                       remote_stack_trace  = info.GetString ("RemoteStackTraceString");
+                       _remoteStackTraceString  = info.GetString ("RemoteStackTraceString");
                        remote_stack_index  = info.GetInt32  ("RemoteStackIndex");
                        hresult             = info.GetInt32  ("HResult");
                        source              = info.GetString ("Source");
                        inner_exception     = (Exception) info.GetValue ("InnerException", typeof (Exception));
+
+                       try {
+                               _data = (IDictionary) info.GetValue ("Data", typeof (IDictionary));
+                       } catch (SerializationException) {
+                               // member did not exist in .NET 1.x
+                       }
                }
 
-               public Exception (string msg, Exception e)
+               public Exception (string message, Exception innerException)
                {
-                       inner_exception = e;
-                       message = msg;
+                       inner_exception = innerException;
+                       this.message = message;
                }
 
                public Exception InnerException {
@@ -112,19 +125,25 @@ namespace System
                        stack_trace = s;
                }
 
+               string ClassName {
+                       get {
+                               if (class_name == null)
+                                       class_name = GetType ().ToString ();
+                               return class_name;
+                       }
+               }
+
                public virtual string Message {
                        get {
                                if (message == null)
-                                       message = string.Format (Locale.GetText ("Exception of type {0} was thrown."), GetType ().ToString());
+                                       message = string.Format (Locale.GetText ("Exception of type '{0}' was thrown."),
+                                               ClassName);
 
                                return message;
                        }
                }
 
                public virtual string Source {
-#if ONLY_1_1
-                       [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
-#endif
                        get {
                                if (source == null) {
                                        StackTrace st = new StackTrace (this, true);
@@ -139,7 +158,7 @@ namespace System
                                        }
                                }
 
-                                // source can be null
+                               // source can be null
                                return source;
                        }
 
@@ -150,22 +169,22 @@ namespace System
 
                public virtual string StackTrace {
                        get {
-                               if (trace_ips == null)
-                                       /* Not thrown yet */
-                                       return null;
-
                                if (stack_trace == null) {
+                                       if (trace_ips == null)
+                                               /* Not thrown yet */
+                                               return null;
+
                                        StackTrace st = new StackTrace (this, 0, true, true);
 
                                        StringBuilder sb = new StringBuilder ();
 
-                                       string newline = String.Format ("{0}{1} ", Environment.NewLine, Locale.GetText ("in"));
+                                       string newline = String.Format ("{0}  {1} ", Environment.NewLine, Locale.GetText ("at"));
                                        string unknown = Locale.GetText ("<unknown method>");
 
                                        for (int i = 0; i < st.FrameCount; i++) {
                                                StackFrame frame = st.GetFrame (i);
                                                if (i == 0)
-                                                       sb.AppendFormat ("{0} ", Locale.GetText ("in"));
+                                                       sb.AppendFormat ("  {0} ", Locale.GetText ("at"));
                                                else
                                                        sb.Append (newline);
 
@@ -176,15 +195,15 @@ namespace System
                                                        else
                                                                sb.AppendFormat ("<0x{0:x5}> {1}", frame.GetNativeOffset (), unknown);
                                                } else {
+                                                       GetFullNameForStackTrace (sb, frame.GetMethod ());
+
                                                        if (frame.GetILOffset () == -1)
-                                                               sb.AppendFormat ("<0x{0:x5}> ", frame.GetNativeOffset ());
+                                                               sb.AppendFormat (" <0x{0:x5}> ", frame.GetNativeOffset ());
                                                        else
-                                                               sb.AppendFormat ("[0x{0:x5}] ", frame.GetILOffset ());
-                                                       string fileName = frame.GetFileName ();
-                                                       if (fileName != null)
-                                                               sb.AppendFormat ("(at {0}:{1}) ", fileName, frame.GetFileLineNumber ());
+                                                               sb.AppendFormat (" [0x{0:x5}] ", frame.GetILOffset ());
 
-                                                       sb.Append (GetFullNameForStackTrace (frame.GetMethod ()));
+                                                       sb.AppendFormat ("in {0}:{1} ", frame.GetSecureFileName (), 
+                                                               frame.GetFileLineNumber ());
                                                }
                                        }
                                        stack_trace = sb.ToString ();
@@ -195,9 +214,6 @@ namespace System
                }
 
                public MethodBase TargetSite {
-#if ONLY_1_1
-                       [ReflectionPermission (SecurityAction.Demand, TypeInformation = true)]
-#endif
                        get {
                                StackTrace st = new StackTrace (this, true);
                                if (st.FrameCount > 0)
@@ -207,9 +223,6 @@ namespace System
                        }
                }
 
-#if NET_2_0
-               private IDictionary _data;
-
                public virtual IDictionary Data {
                        get {
                                if (_data == null) {
@@ -219,7 +232,6 @@ namespace System
                                return _data;
                        }
                }
-#endif
 
                public virtual Exception GetBaseException ()
                {
@@ -236,45 +248,42 @@ namespace System
                        return this;
                }
 
-#if ONLY_1_1
-               [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
-#endif
+               [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
                public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
                {
                        if (info == null)
                                throw new ArgumentNullException ("info");
 
-                       if (class_name == null)
-                               class_name = GetType ().FullName;
-
-                       info.AddValue ("ClassName", class_name);
+                       info.AddValue ("ClassName", ClassName);
                        info.AddValue ("Message", message);
                        info.AddValue ("InnerException", inner_exception);
                        info.AddValue ("HelpURL", help_link);
                        info.AddValue ("StackTraceString", StackTrace);
-                       info.AddValue ("RemoteStackTraceString", remote_stack_trace);
+                       info.AddValue ("RemoteStackTraceString", _remoteStackTraceString);
                        info.AddValue ("RemoteStackIndex", remote_stack_index);
                        info.AddValue ("HResult", hresult);
+#if !MOONLIGHT
                        info.AddValue ("Source", Source);
+#else
+                       info.AddValue ("Source", null);
+#endif
                        info.AddValue ("ExceptionMethod", null);
+                       info.AddValue ("Data", _data, typeof (IDictionary));
                }
 
-#if ONLY_1_1
-               [ReflectionPermission (SecurityAction.Assert, TypeInformation = true)]
-#endif
                public override string ToString ()
                {
-                       System.Text.StringBuilder result = new System.Text.StringBuilder (this.GetType ().FullName);
+                       System.Text.StringBuilder result = new System.Text.StringBuilder (ClassName);
                        result.Append (": ").Append (Message);
 
-                       if (null != remote_stack_trace)
-                               result.Append (remote_stack_trace);
+                       if (null != _remoteStackTraceString)
+                               result.Append (_remoteStackTraceString);
                                
                        if (inner_exception != null) 
                        {
                                result.Append (" ---> ").Append (inner_exception.ToString ());
-                               result.Append (Locale.GetText ("--- End of inner exception stack trace ---"));
                                result.Append (Environment.NewLine);
+                               result.Append (Locale.GetText ("  --- End of inner exception stack trace ---"));
                        }
 
                        if (StackTrace != null)
@@ -289,7 +298,7 @@ namespace System
                                Locale.GetText ("{1}{0}{0}Exception rethrown at [{2}]: {0}");
                        string tmp = String.Format (message, Environment.NewLine, StackTrace, remote_stack_index);
 
-                       remote_stack_trace = tmp;
+                       _remoteStackTraceString = tmp;
                        remote_stack_index++;
 
                        stack_trace = null;
@@ -297,35 +306,51 @@ namespace System
                        return this;
                }
 
-               internal string GetFullNameForStackTrace (MethodBase mi)
+               internal void GetFullNameForStackTrace (StringBuilder sb, MethodBase mi)
                {
-                       string parms = "";
                        ParameterInfo[] p = mi.GetParameters ();
-                       for (int i = 0; i < p.Length; ++i) {
-                               if (i > 0)
-                                       parms = parms + ", ";
-                               string paramName = (p [i].Name == null) ? "" : (" " + p [i].Name);
-                               Type pt = p[i].ParameterType;
-                               if (pt.IsClass && pt.Namespace != "")
-                                       parms = parms + pt.Namespace + "." + pt.Name + paramName;
-                               else
-                                       parms = parms + pt.Name + paramName;
-                       }
+                       sb.Append (mi.DeclaringType.ToString ());
+                       sb.Append (".");
+                       sb.Append (mi.Name);
 
-                       string generic = "";
-#if NET_2_0 || BOOTSTRAP_NET_2_0
-                       if (mi.HasGenericParameters) {
+                       if (mi.IsGenericMethod) {
                                Type[] gen_params = mi.GetGenericArguments ();
-                               generic = "[";
+                               sb.Append ("[");
                                for (int j = 0; j < gen_params.Length; j++) {
                                        if (j > 0)
-                                               generic += ",";
-                                       generic += gen_params [j].Name;
+                                               sb.Append (",");
+                                       sb.Append (gen_params [j].Name);
                                }
-                               generic += "]";
+                               sb.Append ("]");
                        }
-#endif
-                       return mi.DeclaringType.ToString () + ":" + mi.Name + generic + " (" + parms + ")";
-               }                               
+
+                       sb.Append (" (");
+                       for (int i = 0; i < p.Length; ++i) {
+                               if (i > 0)
+                                       sb.Append (", ");
+                               Type pt = p[i].ParameterType;
+                               if (pt.IsClass && pt.Namespace != String.Empty) {
+                                       sb.Append (pt.Namespace);
+                                       sb.Append (".");
+                               }
+                               sb.Append (pt.Name);
+                               if (p [i].Name != null) {
+                                       sb.Append (" ");
+                                       sb.Append (p [i].Name);
+                               }
+                       }
+                       sb.Append (")");
+               }
+
+               //
+               // The documentation states that this is available in 1.x,
+               // but it was not available (MemberRefing this would fail)
+               // and it states the signature is `override sealed', but the
+               // correct value is `newslot' 
+               //
+               public new Type GetType ()
+               {
+                       return base.GetType ();
+               }
        }
 }