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 Microsoft.VisualBasic.CompilerServices;
38 using System.Diagnostics;
40 namespace Microsoft.VisualBasic
42 sealed public class ErrObject {
44 private System.Exception pException;
47 private string pSource;
48 private string pDescription;
49 private string pHelpFile;
50 private int pHelpContext;
52 private int pLastDllError;
54 private bool m_bDontMapException = false;
55 private bool NumberIsSet = false;
56 private bool ClearOnCapture = false;
57 private bool SourceIsSet = false;
58 private bool DescriptionIsSet = false;
59 private bool HelpFileIsSet = false;
60 private bool HelpContextIsSet = false;
77 m_bDontMapException = false;
79 ClearOnCapture = false;
81 DescriptionIsSet = false;
82 HelpFileIsSet = false;
83 HelpContextIsSet = false;
84 ClearOnCapture = true;
87 internal void CaptureException (Exception ex)
91 if(ClearOnCapture == true)
94 ClearOnCapture = true;
99 internal void CaptureException (Exception ex, int lErl)
101 CaptureException(ex);
105 internal Exception CreateException (int Number, String Description)
110 this.Number = Number;
112 throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", "Number"));
113 ex = MapNumberToException(pNumber, Description);
114 this.ClearOnCapture = false;
118 private String FilterDefaultMessage(String Msg)
120 if(pException == null)
123 string Msg1 = Utils.GetResourceString(this.Number);
125 if(Msg == null || Msg.Length == 0)
127 else if(String.CompareOrdinal("Exception from HRESULT: 0x", 0, Msg, 0, System.Math.Min(Msg.Length, 26)) == 0) {
128 return (Msg1 != null ? Msg1 : Msg);
134 private Exception MapNumberToException (int Number, String Description)
136 bool Ignored = false;
137 return ExceptionUtils.BuildException(Number, Description, ref Ignored);
141 // This function needs to be reviewed
142 private int MapExceptionToNumber (Exception ex)
146 if(ex is ArgumentException) {
147 hResult = unchecked((int)0x80070057);
149 else if(ex is ArithmeticException) {
150 if(ex is NotFiniteNumberException) {
151 if((ex as NotFiniteNumberException).OffendingNumber == 0)
157 hResult = unchecked((int)0x80070216);
160 else if(ex is ArrayTypeMismatchException) {
161 hResult = unchecked((int)0x80131503);
163 // else if(exType.Equals(IndexOutOfRangeException)) {
164 // hResult = (exType.Equals(IndexOutOfRangeException)).HResult;
166 else if(ex is InvalidCastException) {
167 hResult = unchecked((int)0x80004002);
169 else if(ex is NotSupportedException) {
170 hResult = unchecked((int)0x80131515);
172 else if(ex is NullReferenceException) {
173 hResult = unchecked((int)0x80004003);
175 else if(ex is UnauthorizedAccessException) {
176 hResult = unchecked((int)0x80131500);
180 hResult = unchecked((int)0x80004005);
183 hResult = ExceptionUtils.getVBFromDotNet(hResult);
190 private void ParseHelpLink (String HelpLink)
194 if(HelpLink == null || HelpLink.Length == 0) {
195 if(HelpContextIsSet == false)
196 this.HelpContext = 0;
198 if (HelpFileIsSet == false)
202 ind = HelpLink.IndexOf("#");
205 if(HelpContextIsSet == false) {
206 if(ind < HelpLink.Length)
207 this.HelpContext = IntegerType.FromString(HelpLink.Substring(ind + 1));
209 this.HelpContext = 0;
211 if (HelpFileIsSet == false)
212 this.HelpFile = HelpLink.Substring(0, ind);
215 if (HelpContextIsSet == false)
216 this.HelpContext = 0;
218 if (!this.HelpFileIsSet)
219 this.HelpFile = HelpLink;
224 internal int MapErrorNumber (int Number)
227 throw new ArgumentException(VBUtils.GetResourceString("Argument_InvalidValue1", "Number"));
233 return ExceptionUtils.fromDotNetToVB(Number);
236 private String MakeHelpLink(String HelpFile, int HelpContext)
238 return HelpFile + "#" + StringType.FromInteger(HelpContext);
242 public void Raise (System.Int32 Number,
243 [System.Runtime.InteropServices.Optional]
244 [System.ComponentModel.DefaultValue(null)] System.Object Source,
245 [System.Runtime.InteropServices.Optional]
246 [System.ComponentModel.DefaultValue(null)] System.Object Description,
247 [System.Runtime.InteropServices.Optional]
248 [System.ComponentModel.DefaultValue(null)] System.Object HelpFile,
249 [System.Runtime.InteropServices.Optional]
250 [System.ComponentModel.DefaultValue(null)] System.Object HelpContext)
255 throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", "Number"));
257 this.Number = Number;
260 this.Source = StringType.FromObject(Source);
262 this.Source = Process.GetCurrentProcess().ProcessName;
265 this.HelpFile = StringType.FromObject(HelpFile);
267 if(HelpContext != null)
268 this.HelpContext = IntegerType.FromObject(HelpContext);
270 if(Description != null)
271 this.Description = StringType.FromObject(Description);
272 else if (DescriptionIsSet == false) {
274 desc = Utils.GetResourceString(pNumber);
277 desc = Utils.GetResourceString("ID95");
279 this.Description = desc;
282 e = MapNumberToException(pNumber, pDescription);
286 e.HelpLink = MakeHelpLink(pHelpFile, pHelpContext);
289 ClearOnCapture = false;
293 internal void SetUnmappedError (int Number)
296 this.Number = Number;
297 ClearOnCapture = false;
300 public System.Exception GetException ()
305 public System.String Description {
310 if(pException == null)
313 this.Description = FilterDefaultMessage(pException.Message);
317 pDescription = value;
318 DescriptionIsSet = true;
322 public System.Int32 Erl {
328 public System.Int32 HelpContext
334 if(pException != null) {
335 ParseHelpLink(pException.HelpLink);
336 return this.pHelpContext;
342 pHelpContext = value;
343 HelpContextIsSet = true;
347 public System.String HelpFile {
349 if (HelpFileIsSet == true)
352 if(pException != null) {
353 ParseHelpLink((pException as Exception).HelpLink);
360 HelpFileIsSet = true;
365 public System.Int32 LastDllError {
371 public System.Int32 Number {
376 if(pException == null)
379 this.Number = MapExceptionToNumber(pException);
383 pNumber = MapErrorNumber(value);
389 public System.String Source {
394 if(pException == null)
397 this.Source = pException.Source;