Merge pull request #1991 from esdrubal/seq_test_fix
[mono.git] / mcs / class / corlib / System / Exception.cs
index 0f0ea62ce9c905abe5245f7c37083d5eba61744c..e07d2e85b62917446160a44c05574c47afa90d0f 100644 (file)
@@ -29,6 +29,7 @@
 //
 
 using System.Collections;
+using System.Collections.Generic;
 using System.Diagnostics;
 using System.Reflection;
 using System.Text;
@@ -43,7 +44,12 @@ namespace System
        [ComVisible(true)]
        [ComDefaultInterface (typeof (_Exception))]
        [ClassInterface (ClassInterfaceType.None)]
+       [StructLayout (LayoutKind.Sequential)]
+#if MOBILE
+       public class Exception : ISerializable
+#else
        public class Exception : ISerializable, _Exception
+#endif
        {
 #pragma warning disable 169, 649
                #region Sync with object-internals.h
@@ -51,7 +57,7 @@ namespace System
                   (vtable/MRGCTX) of the frames. */
                IntPtr [] trace_ips;
                Exception inner_exception;
-               internal string message;
+               internal string _message;
                string help_link;
                string class_name;
                string stack_trace;
@@ -61,8 +67,13 @@ namespace System
                internal int hresult = -2146233088;
                string source;
                IDictionary _data;
+               internal StackTrace[] captured_traces;
+               IntPtr[] native_trace_ips;
+               object dynamic_methods;
                #endregion
-#pragma warning restore 169, 649               
+#pragma warning restore 169, 649
+
+               /* Don't add fields here, the runtime depends on the layout of subclasses */
 
                public Exception ()
                {
@@ -70,7 +81,7 @@ namespace System
 
                public Exception (string message)
                {
-                       this.message = message;
+                       this._message = message;
                }
 
                protected Exception (SerializationInfo info, StreamingContext context)
@@ -79,7 +90,7 @@ namespace System
                                throw new ArgumentNullException ("info");
 
                        class_name          = info.GetString ("ClassName");
-                       message             = info.GetString ("Message");
+                       _message             = info.GetString ("Message");
                        help_link           = info.GetString ("HelpURL");
                        stack_trace         = info.GetString ("StackTraceString");
                        _remoteStackTraceString  = info.GetString ("RemoteStackTraceString");
@@ -98,7 +109,7 @@ namespace System
                public Exception (string message, Exception innerException)
                {
                        inner_exception = innerException;
-                       this.message = message;
+                       this._message = message;
                }
 
                public Exception InnerException {
@@ -110,14 +121,19 @@ namespace System
                        set { help_link = value; }
                }
 
-               protected int HResult {
+               public int HResult {
                        get { return hresult; }
-                       set { hresult = value; }
+                       protected set { hresult = value; }
+               }
+               
+               internal void SetErrorCode(int hr)
+               {
+                       HResult = hr;
                }
 
                internal void SetMessage (string s)
                {
-                       message = s;
+                       _message = s;
                }
 
                internal void SetStackTrace (string s)
@@ -135,11 +151,19 @@ namespace System
 
                public virtual string Message {
                        get {
-                               if (message == null)
-                                       message = string.Format (Locale.GetText ("Exception of type '{0}' was thrown."),
+                               if (_message == null)
+                                       _message = string.Format (Locale.GetText ("Exception of type '{0}' was thrown."),
                                                ClassName);
 
-                               return message;
+                               return _message;
+                       }
+               }
+               
+               [MonoTODO]
+               protected event EventHandler<SafeSerializationEventArgs> SerializeObjectState {
+                       add {
+                       }
+                       remove {
                        }
                }
 
@@ -169,47 +193,15 @@ namespace System
 
                public virtual string StackTrace {
                        get {
-                               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 ("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 ("at"));
-                                               else
-                                                       sb.Append (newline);
-
-                                               if (frame.GetMethod () == null) {
-                                                       string internal_name = frame.GetInternalMethodName ();
-                                                       if (internal_name != null)
-                                                               sb.Append (internal_name);
-                                                       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 ());
-                                                       else
-                                                               sb.AppendFormat (" [0x{0:x5}] ", frame.GetILOffset ());
-
-                                                       sb.AppendFormat ("in {0}:{1} ", frame.GetSecureFileName (), 
-                                                               frame.GetFileLineNumber ());
-                                               }
-                                       }
-                                       stack_trace = sb.ToString ();
-                               }
+                               if (stack_trace != null)
+                                       return stack_trace;
 
-                               return stack_trace;
+                               if (trace_ips == null)
+                                       /* Not thrown yet */
+                                       return null;
+
+                               StackTrace st = new StackTrace (this, 0, true);
+                               return stack_trace = st.ToString ();
                        }
                }
 
@@ -227,7 +219,7 @@ namespace System
                        get {
                                if (_data == null) {
                                        // default to empty dictionary
-                                       _data = (IDictionary) new Hashtable ();
+                                       _data = new Dictionary<object, object> ();
                                }
                                return _data;
                        }
@@ -255,18 +247,14 @@ namespace System
                                throw new ArgumentNullException ("info");
 
                        info.AddValue ("ClassName", ClassName);
-                       info.AddValue ("Message", message);
+                       info.AddValue ("Message", _message);
                        info.AddValue ("InnerException", inner_exception);
                        info.AddValue ("HelpURL", help_link);
                        info.AddValue ("StackTraceString", StackTrace);
                        info.AddValue ("RemoteStackTraceString", _remoteStackTraceString);
                        info.AddValue ("RemoteStackIndex", remote_stack_index);
                        info.AddValue ("HResult", hresult);
-#if !NET_2_1 || MONOTOUCH
                        info.AddValue ("Source", Source);
-#else
-                       info.AddValue ("Source", null);
-#endif
                        info.AddValue ("ExceptionMethod", null);
                        info.AddValue ("Data", _data, typeof (IDictionary));
                }
@@ -306,40 +294,12 @@ namespace System
                        return this;
                }
 
-               internal void GetFullNameForStackTrace (StringBuilder sb, MethodBase mi)
+               // For ExceptionDispatchInfo
+               internal void RestoreExceptionDispatchInfo (System.Runtime.ExceptionServices.ExceptionDispatchInfo exceptionDispatchInfo)
                {
-                       ParameterInfo[] p = mi.GetParameters ();
-                       sb.Append (mi.DeclaringType.ToString ());
-                       sb.Append (".");
-                       sb.Append (mi.Name);
-
-                       if (mi.IsGenericMethod) {
-                               Type[] gen_params = mi.GetGenericArguments ();
-                               sb.Append ("[");
-                               for (int j = 0; j < gen_params.Length; j++) {
-                                       if (j > 0)
-                                               sb.Append (",");
-                                       sb.Append (gen_params [j].Name);
-                               }
-                               sb.Append ("]");
-                       }
-
-                       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 (")");
+                       captured_traces = (StackTrace[]) exceptionDispatchInfo.BinaryStackTraceArray;
+                       trace_ips = null;
+                       stack_trace = null;
                }
 
                //
@@ -352,5 +312,25 @@ namespace System
                {
                        return base.GetType ();
                }
+
+               internal enum ExceptionMessageKind
+               {
+                       ThreadAbort = 1,
+                       ThreadInterrupted = 2,
+                       OutOfMemory = 3
+               }
+
+               internal static String GetMessageFromNativeResources (ExceptionMessageKind kind)
+               {
+                       switch (kind) {
+                       case ExceptionMessageKind.ThreadAbort:
+                               return "";
+                       case ExceptionMessageKind.ThreadInterrupted:
+                               return "";
+                       case ExceptionMessageKind.OutOfMemory:
+                               return "Out of memory";
+                       }
+                       return "";
+               }
        }
 }