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 using Microsoft.VisualBasic.CompilerServices;
14 using System.Diagnostics;
16 namespace Microsoft.VisualBasic
18 sealed public class ErrObject {
20 private System.Exception pException;
23 private string pSource;
24 private string pDescription;
25 private string pHelpFile;
26 private int pHelpContext;
28 private int pLastDllError;
30 private bool m_bDontMapException = false;
31 private bool NumberIsSet = false;
32 private bool ClearOnCapture = false;
33 private bool SourceIsSet = false;
34 private bool DescriptionIsSet = false;
35 private bool HelpFileIsSet = false;
36 private bool HelpContextIsSet = false;
53 m_bDontMapException = false;
55 ClearOnCapture = false;
57 DescriptionIsSet = false;
58 HelpFileIsSet = false;
59 HelpContextIsSet = false;
60 ClearOnCapture = true;
63 internal void CaptureException (Exception ex)
67 if(ClearOnCapture == true)
70 ClearOnCapture = true;
75 internal void CaptureException (Exception ex, int lErl)
81 internal Exception CreateException (int Number, String Description)
88 throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", "Number"));
89 ex = MapNumberToException(pNumber, Description);
90 this.ClearOnCapture = false;
94 private String FilterDefaultMessage(String Msg)
96 if(pException == null)
99 string Msg1 = Utils.GetResourceString(this.Number);
101 if(Msg == null || Msg.Length == 0)
103 else if(String.CompareOrdinal("Exception from HRESULT: 0x", 0, Msg, 0, System.Math.Min(Msg.Length, 26)) == 0) {
104 return (Msg1 != null ? Msg1 : Msg);
110 private Exception MapNumberToException (int Number, String Description)
112 bool Ignored = false;
113 return ExceptionUtils.BuildException(Number, Description, ref Ignored);
117 // This function needs to be reviewed
118 private int MapExceptionToNumber (Exception ex)
122 if(ex is ArgumentException) {
123 hResult = unchecked((int)0x80070057);
125 else if(ex is ArithmeticException) {
126 if(ex is NotFiniteNumberException) {
127 if((ex as NotFiniteNumberException).OffendingNumber == 0)
133 hResult = unchecked((int)0x80070216);
136 else if(ex is ArrayTypeMismatchException) {
137 hResult = unchecked((int)0x80131503);
139 // else if(exType.Equals(IndexOutOfRangeException)) {
140 // hResult = (exType.Equals(IndexOutOfRangeException)).HResult;
142 else if(ex is InvalidCastException) {
143 hResult = unchecked((int)0x80004002);
145 else if(ex is NotSupportedException) {
146 hResult = unchecked((int)0x80131515);
148 else if(ex is NullReferenceException) {
149 hResult = unchecked((int)0x80004003);
151 else if(ex is UnauthorizedAccessException) {
152 hResult = unchecked((int)0x80131500);
156 hResult = unchecked((int)0x80004005);
159 hResult = ExceptionUtils.getVBFromDotNet(hResult);
166 private void ParseHelpLink (String HelpLink)
170 if(HelpLink == null || HelpLink.Length == 0) {
171 if(HelpContextIsSet == false)
172 this.HelpContext = 0;
174 if (HelpFileIsSet == false)
178 ind = HelpLink.IndexOf("#");
181 if(HelpContextIsSet == false) {
182 if(ind < HelpLink.Length)
183 this.HelpContext = IntegerType.FromString(HelpLink.Substring(ind + 1));
185 this.HelpContext = 0;
187 if (HelpFileIsSet == false)
188 this.HelpFile = HelpLink.Substring(0, ind);
191 if (HelpContextIsSet == false)
192 this.HelpContext = 0;
194 if (!this.HelpFileIsSet)
195 this.HelpFile = HelpLink;
200 internal int MapErrorNumber (int Number)
203 throw new ArgumentException(VBUtils.GetResourceString("Argument_InvalidValue1", "Number"));
209 return ExceptionUtils.fromDotNetToVB(Number);
212 private String MakeHelpLink(String HelpFile, int HelpContext)
214 return HelpFile + "#" + StringType.FromInteger(HelpContext);
218 public void Raise (System.Int32 Number,
219 [System.Runtime.InteropServices.Optional]
220 [System.ComponentModel.DefaultValue(null)] System.Object Source,
221 [System.Runtime.InteropServices.Optional]
222 [System.ComponentModel.DefaultValue(null)] System.Object Description,
223 [System.Runtime.InteropServices.Optional]
224 [System.ComponentModel.DefaultValue(null)] System.Object HelpFile,
225 [System.Runtime.InteropServices.Optional]
226 [System.ComponentModel.DefaultValue(null)] System.Object HelpContext)
231 throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", "Number"));
233 this.Number = Number;
236 this.Source = StringType.FromObject(Source);
238 this.Source = Process.GetCurrentProcess().ProcessName;
241 this.HelpFile = StringType.FromObject(HelpFile);
243 if(HelpContext != null)
244 this.HelpContext = IntegerType.FromObject(HelpContext);
246 if(Description != null)
247 this.Description = StringType.FromObject(Description);
248 else if (DescriptionIsSet == false) {
250 desc = Utils.GetResourceString(pNumber);
253 desc = Utils.GetResourceString("ID95");
255 this.Description = desc;
258 e = MapNumberToException(pNumber, pDescription);
262 e.HelpLink = MakeHelpLink(pHelpFile, pHelpContext);
265 ClearOnCapture = false;
269 internal void SetUnmappedError (int Number)
272 this.Number = Number;
273 ClearOnCapture = false;
276 public System.Exception GetException ()
281 public System.String Description {
286 if(pException == null)
289 this.Description = FilterDefaultMessage(pException.Message);
293 pDescription = value;
294 DescriptionIsSet = true;
298 public System.Int32 Erl {
304 public System.Int32 HelpContext
310 if(pException != null) {
311 ParseHelpLink(pException.HelpLink);
312 return this.pHelpContext;
318 pHelpContext = value;
319 HelpContextIsSet = true;
323 public System.String HelpFile {
325 if (HelpFileIsSet == true)
328 if(pException != null) {
329 ParseHelpLink((pException as Exception).HelpLink);
336 HelpFileIsSet = true;
341 public System.Int32 LastDllError {
347 public System.Int32 Number {
352 if(pException == null)
355 this.Number = MapExceptionToNumber(pException);
359 pNumber = MapErrorNumber(value);
365 public System.String Source {
370 if(pException == null)
373 this.Source = pException.Source;