5 // Chris J Breisch (cjbreisch@altavista.net)
6 // Francesco Delfino (pluto@tipic.com)
8 // (C) 2002 Chris J Breisch
9 // 2002 pluto@tipic.com
13 // Copyright (c) 2002-2003 Mainsoft Corporation.
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 using System
.Runtime
.InteropServices
;
38 using Microsoft
.VisualBasic
.CompilerServices
;
39 using System
.Diagnostics
;
41 namespace Microsoft
.VisualBasic
43 sealed public class ErrObject
{
45 private System
.Exception pException
;
48 private string pSource
;
49 private string pDescription
;
50 private string pHelpFile
;
51 private int pHelpContext
;
53 private int pLastDllError
;
55 private bool m_bDontMapException
= false;
56 private bool NumberIsSet
= false;
57 private bool ClearOnCapture
= false;
58 private bool SourceIsSet
= false;
59 private bool DescriptionIsSet
= false;
60 private bool HelpFileIsSet
= false;
61 private bool HelpContextIsSet
= false;
78 m_bDontMapException
= false;
80 ClearOnCapture
= false;
82 DescriptionIsSet
= false;
83 HelpFileIsSet
= false;
84 HelpContextIsSet
= false;
85 ClearOnCapture
= true;
88 internal void CaptureException (Exception ex
)
92 if(ClearOnCapture
== true)
95 ClearOnCapture
= true;
100 internal void CaptureException (Exception ex
, int lErl
)
102 CaptureException(ex
);
106 internal Exception
CreateException (int Number
, String Description
)
111 this.Number
= Number
;
113 throw new ArgumentException(Utils
.GetResourceString("Argument_InvalidValue1", "Number"));
114 ex
= MapNumberToException(pNumber
, Description
);
115 this.ClearOnCapture
= false;
119 private String
FilterDefaultMessage(String Msg
)
121 if(pException
== null)
124 string Msg1
= Utils
.GetResourceString(this.Number
);
126 if(Msg
== null || Msg
.Length
== 0)
128 else if(String
.CompareOrdinal("Exception from HRESULT: 0x", 0, Msg
, 0, System
.Math
.Min(Msg
.Length
, 26)) == 0) {
129 return (Msg1
!= null ? Msg1
: Msg
);
135 private Exception
MapNumberToException (int Number
, String Description
)
137 bool Ignored
= false;
138 return ExceptionUtils
.BuildException(Number
, Description
, ref Ignored
);
142 // This function needs to be reviewed
143 private int MapExceptionToNumber (Exception ex
)
147 if(ex
is ArgumentException
) {
148 hResult
= unchecked((int)0x80070057);
150 else if(ex
is ArithmeticException
) {
151 if(ex
is NotFiniteNumberException
) {
152 if((ex
as NotFiniteNumberException
).OffendingNumber
== 0)
158 hResult
= unchecked((int)0x80070216);
161 else if(ex
is ArrayTypeMismatchException
) {
162 hResult
= unchecked((int)0x80131503);
164 // else if(exType.Equals(IndexOutOfRangeException)) {
165 // hResult = (exType.Equals(IndexOutOfRangeException)).HResult;
167 else if(ex
is InvalidCastException
) {
168 hResult
= unchecked((int)0x80004002);
170 else if(ex
is NotSupportedException
) {
171 hResult
= unchecked((int)0x80131515);
173 else if(ex
is NullReferenceException
) {
174 hResult
= unchecked((int)0x80004003);
176 else if(ex
is UnauthorizedAccessException
) {
177 hResult
= unchecked((int)0x80131500);
181 hResult
= unchecked((int)0x80004005);
184 hResult
= ExceptionUtils
.getVBFromDotNet(hResult
);
191 private void ParseHelpLink (String HelpLink
)
195 if(HelpLink
== null || HelpLink
.Length
== 0) {
196 if(HelpContextIsSet
== false)
197 this.HelpContext
= 0;
199 if (HelpFileIsSet
== false)
203 ind
= HelpLink
.IndexOf("#");
206 if(HelpContextIsSet
== false) {
207 if(ind
< HelpLink
.Length
)
208 this.HelpContext
= IntegerType
.FromString(HelpLink
.Substring(ind
+ 1));
210 this.HelpContext
= 0;
212 if (HelpFileIsSet
== false)
213 this.HelpFile
= HelpLink
.Substring(0, ind
);
216 if (HelpContextIsSet
== false)
217 this.HelpContext
= 0;
219 if (!this.HelpFileIsSet
)
220 this.HelpFile
= HelpLink
;
225 internal int MapErrorNumber (int Number
)
228 throw new ArgumentException(VBUtils
.GetResourceString("Argument_InvalidValue1", "Number"));
234 return ExceptionUtils
.fromDotNetToVB(Number
);
237 private String
MakeHelpLink(String HelpFile
, int HelpContext
)
239 return HelpFile
+ "#" + StringType
.FromInteger(HelpContext
);
243 public void Raise (System
.Int32 Number
,
244 [Optional
, __DefaultArgumentValue(null)] System
.Object Source
,
245 [Optional
, __DefaultArgumentValue(null)] System
.Object Description
,
246 [Optional
, __DefaultArgumentValue(null)] System
.Object HelpFile
,
247 [Optional
, __DefaultArgumentValue(null)] System
.Object HelpContext
)
252 throw new ArgumentException(Utils
.GetResourceString("Argument_InvalidValue1", "Number"));
254 this.Number
= Number
;
257 this.Source
= StringType
.FromObject(Source
);
259 this.Source
= Process
.GetCurrentProcess().ProcessName
;
262 this.HelpFile
= StringType
.FromObject(HelpFile
);
264 if(HelpContext
!= null)
265 this.HelpContext
= IntegerType
.FromObject(HelpContext
);
267 if(Description
!= null)
268 this.Description
= StringType
.FromObject(Description
);
269 else if (DescriptionIsSet
== false) {
271 desc
= Utils
.GetResourceString(pNumber
);
274 desc
= Utils
.GetResourceString("ID95");
276 this.Description
= desc
;
279 e
= MapNumberToException(pNumber
, pDescription
);
283 e
.HelpLink
= MakeHelpLink(pHelpFile
, pHelpContext
);
286 ClearOnCapture
= false;
290 internal void SetUnmappedError (int Number
)
293 this.Number
= Number
;
294 ClearOnCapture
= false;
297 public System
.Exception
GetException ()
302 public System
.String Description
{
307 if(pException
== null)
310 this.Description
= FilterDefaultMessage(pException
.Message
);
314 pDescription
= value;
315 DescriptionIsSet
= true;
319 public System
.Int32 Erl
{
325 public System
.Int32 HelpContext
331 if(pException
!= null) {
332 ParseHelpLink(pException
.HelpLink
);
333 return this.pHelpContext
;
339 pHelpContext
= value;
340 HelpContextIsSet
= true;
344 public System
.String HelpFile
{
346 if (HelpFileIsSet
== true)
349 if(pException
!= null) {
350 ParseHelpLink((pException
as Exception
).HelpLink
);
357 HelpFileIsSet
= true;
362 public System
.Int32 LastDllError
{
368 public System
.Int32 Number
{
373 if(pException
== null)
376 this.Number
= MapExceptionToNumber(pException
);
380 pNumber
= MapErrorNumber(value);
386 public System
.String Source
{
391 if(pException
== null)
394 this.Source
= pException
.Source
;