Hi Don,
Managed and unmanaged code can work together to handle exceptions. If a
method throws an exception in managed code, the common language runtime can
pass an HRESULT to a COM object. If a method fails in unmanaged code by
returning a failure HRESULT, the runtime throws an exception that can be
caught by managed code.
The runtime automatically maps the HRESULT from COM interop to more
specific exceptions. For example, E_ACCESSDENIED becomes
UnauthorizedAccessException, E_OUTOFMEMORY becomes OutOfMemoryException,
and so on.
If the HRESULT is a custom result or if it is unknown to the runtime, the
runtime passes a generic COMException to the client. The ErrorCode property
of the COMException contains the HRESULT value.
In your specific case, the behavior is correct since the error returned
from MailItem.SaveAs itself doesn't contain useful information. You can
verify this by creating a simple macro in Outlook VBA and the error will be
the same "The operation failed":
Sub test()
Dim f As Folder
Set f = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
Dim i As MailItem
For Each i In f.Items
i.SaveAs ("c:::1")
Exit For
Next
End Sub
Exception thrown from COM should be available in .NET through the
IErrorInfo interface automatically. For example, if you create following
COM component in VB6 which throws a exception:
Public Sub Test()
Err.Raise 12345, "Project1.Class1.Test()", "Custom exception from VB6"
End Sub
When you call it from VB.NET:
Try
Dim x As New Project1.Class1
x.Test()
Catch e As Exception
If TypeOf e Is System.Runtime.InteropServices.COMException Then
Dim ce As System.Runtime.InteropServices.COMException =
CType(e, System.Runtime.InteropServices.COMException)
Stop
End If
End Try
You will see:
e.Message = "Custom exception from VB6"
e.Source = "Project1.Class1.Test()"
ce.ErrorCode = &H800A3039
The error code is composed of several parts, the last part is 12345
(&H3039):
#Heath Stewart's Blog : Deciphering an HRESULT
http://blogs.msdn.com/heaths/archive/2005/07/21/441391.aspx
For more information why we need to check the exception type is a
COMException or not:
#Handling COM Interop Exceptions (.NET Framework Developer's Guide)
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconhandlingcominterop
exceptions.asp?frame=true
Hope this helps.
Sincerely,
Walter Wang (
[email protected], remove 'online.')
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.