The Labs \ Source Viewer \ SSCLI \ System.Runtime.InteropServices \ COMException

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*=============================================================================
  16. **
  17. ** Class: COMException
  18. **
  19. **
  20. ** Purpose: Exception class for all errors from COM Interop where we don't
  21. ** recognize the HResult.
  22. **
  23. **
  24. =============================================================================*/
  25. namespace System.Runtime.InteropServices
  26. {
  27.     using System.Runtime.InteropServices;
  28.     using System;
  29.     using System.Runtime.Serialization;
  30.     using System.Globalization;
  31.     // Exception for COM Interop errors where we don't recognize the HResult.
  32.     //
  33.     [System.Runtime.InteropServices.ComVisible(true)]
  34.     [Serializable()]
  35.     public class COMException : ExternalException
  36.     {
  37.         public COMException() : base(Environment.GetResourceString("Arg_COMException"))
  38.         {
  39.             SetErrorCode(__HResults.E_FAIL);
  40.         }
  41.        
  42.         public COMException(string message) : base(message)
  43.         {
  44.             SetErrorCode(__HResults.E_FAIL);
  45.         }
  46.        
  47.         public COMException(string message, Exception inner) : base(message, inner)
  48.         {
  49.             SetErrorCode(__HResults.E_FAIL);
  50.         }
  51.        
  52.         public COMException(string message, int errorCode) : base(message)
  53.         {
  54.             SetErrorCode(errorCode);
  55.         }
  56.        
  57.         protected COMException(SerializationInfo info, StreamingContext context) : base(info, context)
  58.         {
  59.         }
  60.        
  61.         public override string ToString()
  62.         {
  63.             string message = Message;
  64.             string s;
  65.             string _className = GetType().ToString();
  66.             s = _className + " (0x" + HResult.ToString("X8", CultureInfo.InvariantCulture) + ")";
  67.            
  68.             if (!(message == null || message.Length <= 0)) {
  69.                 s = s + ": " + message;
  70.             }
  71.            
  72.             Exception _innerException = InnerException;
  73.            
  74.             if (_innerException != null) {
  75.                 s = s + " ---> " + _innerException.ToString();
  76.             }
  77.            
  78.            
  79.             if (StackTrace != null)
  80.                 s += Environment.NewLine + StackTrace;
  81.            
  82.             return s;
  83.         }
  84.        
  85.        
  86.     }
  87. }

Developer Fusion