1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 using System;
6 using System.Globalization;
7 using System.Runtime.Serialization;
8 using System.Diagnostics;
9 
10 namespace IceInternal
11 {
12     public class Ex
13     {
throwUOE(Type expectedType, Ice.Value v)14         public static void throwUOE(Type expectedType, Ice.Value v)
15         {
16             //
17             // If the object is an unknown sliced object, we didn't find an
18             // value factory, in this case raise a NoValueFactoryException
19             // instead.
20             //
21             if(v is Ice.UnknownSlicedValue)
22             {
23                 Ice.UnknownSlicedValue usv = (Ice.UnknownSlicedValue)v;
24                 throw new Ice.NoValueFactoryException("", usv.ice_id());
25             }
26 
27             string type = v.ice_id();
28             string expected;
29             try
30             {
31                 expected = (string)expectedType.GetMethod("ice_staticId").Invoke(null, null);
32             }
33             catch(Exception)
34             {
35                 expected = "";
36                 Debug.Assert(false);
37             }
38 
39             throw new Ice.UnexpectedObjectException("expected element of type `" + expected + "' but received `" +
40                                                     type + "'", type, expected);
41         }
42 
throwMemoryLimitException(int requested, int maximum)43         public static void throwMemoryLimitException(int requested, int maximum)
44         {
45             throw new Ice.MemoryLimitException("requested " + requested + " bytes, maximum allowed is " + maximum +
46                                                " bytes (see Ice.MessageSizeMax)");
47         }
48     }
49 }
50 
51 namespace Ice
52 {
53     /// <summary>
54     /// Base class for Ice exceptions.
55     /// </summary>
56     [Serializable]
57     public abstract class Exception : System.Exception, ICloneable
58     {
59         /// <summary>
60         /// Creates and returns a copy of this exception.
61         /// </summary>
62         /// <returns>A copy of this exception.</returns>
Clone()63         public object Clone()
64         {
65             return MemberwiseClone();
66         }
67 
68         /// <summary>
69         /// Creates a default-initialized exception.
70         /// </summary>
Exception()71         public Exception() {}
72 
73         /// <summary>
74         /// Creates a default-initialized exception and sets the InnerException
75         /// property to the passed exception.
76         /// </summary>
77         /// <param name="ex">The inner exception.</param>
Exception(System.Exception ex)78         public Exception(System.Exception ex) : base("", ex) {}
79 
80         /// <summary>
81         /// Initializes a new instance of the exception with serialized data.
82         /// </summary>
83         /// <param name="info">Holds the serialized object data about the exception being thrown.</param>
84         /// <param name="context">Contains contextual information about the source or destination.</param>
Exception(SerializationInfo info, StreamingContext context)85         protected Exception(SerializationInfo info, StreamingContext context) : base(info, context) {}
86 
87         /// <summary>
88         /// ice_name() is deprecated, use ice_id() instead.
89         /// Returns the name of this exception.
90         /// </summary>
91         /// <returns>The name of this exception.</returns>
92         [Obsolete("ice_name() is deprecated, use ice_id() instead.")]
ice_name()93         public string ice_name()
94         {
95             return ice_id().Substring(2);
96         }
97 
98         /// <summary>
99         /// Returns the type id of this exception.
100         /// </summary>
101         /// <returns>The type id of this exception.</returns>
ice_id()102         public abstract string ice_id();
103 
104         /// <summary>
105         /// Returns a string representation of this exception, including
106         /// any inner exceptions.
107         /// </summary>
108         /// <returns>The string representation of this exception.</returns>
ToString()109         public override string ToString()
110         {
111             //
112             // This prints the exception Java style. That is, the outermost
113             // exception, "Caused by:" to the innermost exception. The
114             // stack trace is not nicely indented as with Java, but
115             // without string parsing (perhaps tokenize on "\n"), it
116             // doesn't appear to be possible to reformat it.
117             //
118             System.IO.StringWriter sw = new System.IO.StringWriter(CultureInfo.CurrentCulture);
119             IceUtilInternal.OutputBase op = new IceUtilInternal.OutputBase(sw);
120             op.setUseTab(false);
121             op.print(GetType().FullName);
122             op.inc();
123             IceInternal.ValueWriter.write(this, op);
124             sw.Write("\n");
125             sw.Write(StackTrace);
126 
127             System.Exception curr = InnerException;
128             while(curr != null)
129             {
130                 sw.Write("\nCaused by: ");
131                 sw.Write(curr.GetType().FullName);
132                 if(!(curr is Ice.Exception))
133                 {
134                     sw.Write(": ");
135                     sw.Write(curr.Message);
136                 }
137                 sw.Write("\n");
138                 sw.Write(curr.StackTrace);
139                 curr = curr.InnerException;
140             }
141 
142             return sw.ToString();
143         }
144     }
145 
146     /// <summary>
147     /// Base class for Ice run-time exceptions.
148     /// </summary>
149     [Serializable]
150     public abstract class LocalException : Exception
151     {
152         /// <summary>
153         /// Creates a default-initialized Ice run-time exception.
154         /// </summary>
LocalException()155         public LocalException() {}
156 
157         /// <summary>
158         /// Creates a default-initialized Ice run-time exception and sets the InnerException
159         /// property to the passed exception.
160         /// </summary>
161         /// <param name="ex">The inner exception.</param>
LocalException(System.Exception ex)162         public LocalException(System.Exception ex) : base(ex) {}
163 
164         /// <summary>
165         /// Initializes a new instance of the exception with serialized data.
166         /// </summary>
167         /// <param name="info">Holds the serialized object data about the exception being thrown.</param>
168         /// <param name="context">Contains contextual information about the source or destination.</param>
LocalException(SerializationInfo info, StreamingContext context)169         protected LocalException(SerializationInfo info, StreamingContext context) : base(info, context) {}
170     }
171 
172     /// <summary>
173     /// Base class for Ice system exceptions.
174     /// Ice system exceptions are currently Ice internal, non-documented exceptions.
175     /// </summary>
176     [Serializable]
177     public abstract class SystemException : Exception
178     {
179         /// <summary>
180         /// Creates a default-initialized system exception.
181         /// </summary>
SystemException()182         public SystemException() {}
183 
184         /// <summary>
185         /// Creates a default-initialized system exception and sets the InnerException
186         /// property to the passed exception.
187         /// </summary>
188         /// <param name="ex">The inner exception.</param>
SystemException(System.Exception ex)189         public SystemException(System.Exception ex) : base(ex) {}
190 
191         /// <summary>
192         /// Initializes a new instance of the exception with serialized data.
193         /// </summary>
194         /// <param name="info">Holds the serialized object data about the exception being thrown.</param>
195         /// <param name="context">Contains contextual information about the source or destination.</param>
SystemException(SerializationInfo info, StreamingContext context)196         protected SystemException(SerializationInfo info, StreamingContext context) : base(info, context) {}
197     }
198 
199     /// <summary>
200     /// Base class for Slice user exceptions.
201     /// </summary>
202     [Serializable]
203     public abstract class UserException : Exception
204     {
205         /// <summary>
206         /// Creates a default-initialized user exception.
207         /// </summary>
UserException()208         public UserException() {}
209 
210         /// <summary>
211         /// Creates a default-initialized user exception and sets the InnerException
212         /// property to the passed exception.
213         /// </summary>
214         /// <param name="ex">The inner exception.</param>
UserException(System.Exception ex)215         public UserException(System.Exception ex) : base(ex) {}
216 
217         /// <summary>
218         /// Initializes a new instance of the exception with serialized data.
219         /// </summary>
220         /// <param name="info">Holds the serialized object data about the exception being thrown.</param>
221         /// <param name="context">Contains contextual information about the source or destination.</param>
UserException(SerializationInfo info, StreamingContext context)222         protected UserException(SerializationInfo info, StreamingContext context) : base(info, context) {}
223 
224         /// <summary>
225         /// Returns the sliced data if the exception has a preserved-slice base class and has been sliced during
226         /// un-marshaling, null is returned otherwise.
227         /// </summary>
228         /// <returns>The sliced data or null.</returns>
ice_getSlicedData()229         public virtual Ice.SlicedData ice_getSlicedData()
230         {
231             return null;
232         }
233 
iceWrite(OutputStream ostr)234         public virtual void iceWrite(OutputStream ostr)
235         {
236             ostr.startException(null);
237             iceWriteImpl(ostr);
238             ostr.endException();
239         }
240 
iceRead(InputStream istr)241         public virtual void iceRead(InputStream istr)
242         {
243             istr.startException();
244             iceReadImpl(istr);
245             istr.endException(false);
246         }
247 
iceUsesClasses()248         public virtual bool iceUsesClasses()
249         {
250             return false;
251         }
252 
iceWriteImpl(OutputStream ostr)253         protected abstract void iceWriteImpl(OutputStream ostr);
iceReadImpl(InputStream istr)254         protected abstract void iceReadImpl(InputStream istr);
255     }
256 }
257 
258 namespace IceInternal
259 {
260     public class RetryException : Exception
261     {
RetryException(Ice.LocalException ex)262         public RetryException(Ice.LocalException ex)
263         {
264             _ex = ex;
265         }
266 
get()267         public Ice.LocalException get()
268         {
269             return _ex;
270         }
271 
272         private Ice.LocalException _ex;
273     }
274 }
275