有关异常错误的更多详细信息? [英] More detailed information about Exception Error?

查看:108
本文介绍了有关异常错误的更多详细信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用Visual Basic .NET并引用Interop.Outlook时,是否有一种

方式来获取有关错误的详细信息,而不是

Exception.Message还是Exception.ToString?例如,Outlook的

MailItem.SaveAs方法可以返回错误消息操作

失败,但我不知道这意味着什么。作为测试,我尝试使用含有非法字符的

a文件名并获得错误消息

内部应用程序错误,这对解释这个没有帮助

特别错误。我想我可以使用一系列Catch e As

派生的异常类型,但我甚至不确定包含什么。相反,它可以是真实的。只是在一些与基础相关的属性中找到错误

异常类对象?谢谢!

解决方案

嗨Don,


托管代码和非托管代码可以一起处理异常。如果

方法在托管代码中抛出异常,则公共语言运行库可以将
传递给HRES对象。如果一个方法在非托管代码中失败,那么返回一个失败的HRESULT,运行时抛出一个异常,可以是托管代码捕获的



运行时自动将HRESULT从COM interop映射到更多

特定异常。例如,E_ACCESSDENIED变为

UnauthorizedAccessException,E_OUTOFMEMORY变为OutOfMemoryException,

等等。


如果HRESULT是自定义结果或者如果运行时未知,则

运行时将通用COMException传递给客户端。 COMException的ErrorCode属性

包含HRESULT值。


在特定情况下,行为正确,因为错误返回
$ b来自MailItem.Save的$ b本身并不包含有用的信息。您可以通过在Outlook VBA中创建一个简单的宏来验证这一点,并且错误将是相同的操作失败:


子测试()

Dim f As文件夹

设置f = Application.GetNamespace(" MAPI")。GetDefaultFolder(olFolderInbox)

Dim i As MailItem

For each i in f.Items

i.SaveAs(" c ::: 1")

退出

下一页

结束子

COM中抛出的异常应该可以在.NET中通过

获得IErrorInfo界面自动生成。例如,如果你在VB6中创建以下

COM组件,它会引发异常:


Public Sub Test()

Err .Raise 12345,Project1.Class1.Test(),来自VB6的自定义异常

结束子


当你从VB.NET:


尝试

Dim x As New Project1.Class1

x.Test()

Catch e As Exception

如果TypeOf e是System.Runtime.InteropServices.COMException那么

Dim ce As System.Runtime.InteropServices.COMException =

CType(e,System.Runtime.InteropServices.COMException)

停止

结束如果

结束尝试


你会看到:


e.Message ="来自VB6的自定义异常"

e.Source =" Project1。 Class1.Test()"

ce.ErrorCode =& H800A3039

错误代码由几部分组成,最后一部分是12345

(& H3039):


#Heath Stewart'的博客:解密一个HRESULT
http://blogs.msdn.com/heaths/archive...21/441391.aspx

有关我们需要检查的更多信息异常类型是否是
COMException:


#Handling COM Interop异常(.NET Framework开发人员指南)
http://msdn.microsoft.com/library/en。 ..lingcominterop

exceptions.asp?frame = true

希望这会有所帮助。


此致,

Walter Wang(wa****@online.microsoft.com,删除''在线。'')

Microsoft在线社区支持


=============================================== ===

通过电子邮件收到我的帖子通知?请参阅
http://msdn.microsoft .com / subscripti ... ult.aspx#notif

ications。如果您使用的是Outlook Express,请确保清除

复选框工具/选项/读取:一次获取300个标题及时看到你的回复




注意:MSDN托管新闻组支持服务是针对非紧急问题

其中来自社区或Microsoft支持的初步响应

工程师可在1个工作日内完成。请注意,每个跟随

的响应可能需要大约2个工作日作为支持

专业人士与您合作可能需要进一步调查才能达到

最有效的分辨率。该产品不适用于需要紧急,实时或基于电话的交互或复杂的b $ b项目分析和转储分析问题的情况。这种性质的问题最好通过联系

Microsoft客户支持服务(CSS)处理
href =http://msdn.microsoft.com/subscriptions/support/default.aspx\"target =_ blank> http://msdn.microsoft.com/subscripti...t/default.aspx

======================================== ==========


此帖子按原样提供。没有保证,也没有授予任何权利。


嗨Walter,


还有问题...我的测试用例调用MailItem.Save使用具有非法字符的

文件名。 VB6中返回的错误代码是

x800300FC,其描述为内部应用程序错误。我确实发现

将该代码传递给FormatMessage返回名称%1无效

更有用(需要使用

FORMAT_MESSAGE_IGNORE_INSERTS标志),所以我会在VB.NET中这样做。但每次运行

应用程序时,COMException.ErrorCode中返回的

HRESULT都不同。我已经获得了xA72300FC,xA0F300FC,xA30300FC,xA51300FC等。

而300FC则是每个HRESULT的一部分与300FC相匹配。

VB6错误代码的一部分,其余部分没有,因此FormatMessage中的查找失败。一个

的解决方案是使用x800FFFFF来进行HRESULT的逐位运算,

为每个不同的HRESULT生成x800300FC的正确代码
COMException返回
。这是一个很好的解决方案,还是有另一个

方法,我应该用它来导出适当的错误代码用于

FormatMessage?另外,如果使用AND是正确的,那么x800FFFFF是否可以,或者

保留两个严重性位是否为xC00FFFFF?


感谢您的帮助,

Don

Walter Wang [MSFT]" < wa **** @ online.microsoft.comwrote ...


>

COM中抛出的异常应该可用于.NET自动通过

IErrorInfo界面。例如,如果你在VB6中创建以下

COM组件,它会引发异常:


Public Sub Test()

Err .Raise 12345,Project1.Class1.Test(),来自VB6的自定义异常

结束子


当你从VB.NET:


尝试

Dim x As New Project1.Class1

x.Test()

Catch e As Exception

如果TypeOf e是System.Runtime.InteropServices.COMException



那么


Dim ce As System.Runtime.InteropServices.COMException =

CType(e,System.Runtime.InteropServices.COMException)

Stop

结束如果

结束尝试


你会看到:


e.Message =" VB6中的自定义异常"

e.Source =" Project1.Class1.Test()" ;

ce.ErrorCode =& H800A3039


错误代码由几部分组成,最后一部分是12345

(& H3039):


希望这会有所帮助。


此致,

Walter Wang(wa ** ** @ online.microsoft.com,删除''在线。'')

Microsoft在线社区支持



< blockquote>嗨Don,


HRESULT由以下字段组成:


*表示严重性的1位代码,其中0表示成功和1

表示失败。

* 4位保留值。

* 11位代码表示对错误负责或警告,还有

称为设施代码。

*描述错误或警告的16位代码。


# HRESULT
http://msdn2.microsoft.com /en-us/library/ms526450.aspx

如果出现COMException,您可以使用Marshal.GetExceptionForHR将HRESULT再次转换为异常。它将返回正确的消息:

导入Microsoft.Office.Interop.Outlook

导入System.Runtime.InteropServices


模块模块1

Sub Main()

昏暗的应用程序作为新的应用程序()

昏暗的收件箱作为MAPIFolder =

app.GetNamespace(" MAPI")。GetDefaultFolder(OlDefaul tFolders.olFolderInbox)

For Each item As MailItem in inbox.Items

试试

item.SaveAs("?。msg")

Catch e As System.Exception

如果TypeOf e是COMException那么

Dim ce由于COMException = CType(e,COMException)

Dim e2 As System.Exception =

Marshal.GetExceptionForHR(ce.ErrorCode)

控制台。写(e2.Message)

否则

Console.Write(e.Message)

结束如果

结束试试

退出

下一页

结束子


结束模块

希望这会有所帮助。


问候,

Walter Wang (wa****@online.microsoft.com,删除''在线。'')

Microsoft在线社区支持


===== =============================================

在回复帖子时,请回复群组通过你的新闻阅读器

其他人可以从你的问题中学习并从中受益。

==================== ==============================

此帖子提供按现状 ;没有保证,也没有授予任何权利。


When using Visual Basic .NET with a reference to Interop.Outlook, is there a
way to get more detailed information about an error other than
Exception.Message or Exception.ToString? For example, Outlook''s
MailItem.SaveAs method can return an error message of "The operation
failed", but I have no idea what that means. As a test, I tried SaveAs using
a filename that contained illegal characters and got the error message
"Internal Application Error", which isn''t helpful for explaining that
particular error either. I suppose I could use a series of "Catch e As"
derived Exception Types, but I''m not even sure what to include. Instead, can
the "real" error simply be found in some property related to the base
Exception Class object? Thanks!

解决方案

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...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...lingcominterop
exceptions.asp?frame=true
Hope this helps.

Sincerely,
Walter Wang (wa****@online.microsoft.com, remove ''online.'')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


Hi Walter,

Still having a problem... My test case calls MailItem.SaveAs using a
filename having illegal characters. The error code returned in VB6 is
x800300FC with a description of "Internal Application Error". I did find
that passing that code to FormatMessage returns "The name %1 is not valid"
which is much more useful (requires the use of the
FORMAT_MESSAGE_IGNORE_INSERTS flag), so I''ll do that in VB.NET. But the
HRESULT returned in COMException.ErrorCode is different every time I run the
application. I''ve gotten xA72300FC, xA0F300FC, xA30300FC, xA51300FC, etc.
While the "300FC" portion of each HRESULT matches the "300FC" portion of the
VB6 Error Code, the rest does not, so the lookup in FormatMessage fails. One
solution apears to be to do a bit-wise AND of the HRESULT using x800FFFFF,
which results in the proper code of x800300FC for each different HRESULT
returned by the COMException. Is that a good solution, or is there another
method that I should use to derive the proper Error Code for use with
FormatMessage? Also, if using AND is correct, then is x800FFFFF okay, or to
preserve both severity bits should it be xC00FFFFF?

Thanks for your help,
Don
"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote...

>
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):

Hope this helps.

Sincerely,
Walter Wang (wa****@online.microsoft.com, remove ''online.'')
Microsoft Online Community Support



Hi Don,

A HRESULT is made up of following fields:

* A 1-bit code indicating severity, where zero represents success and 1
represents failure.
* A 4-bit reserved value.
* An 11-bit code indicating responsibility for the error or warning, also
known as a facility code.
* A 16-bit code describing the error or warning.

#HRESULT
http://msdn2.microsoft.com/en-us/library/ms526450.aspx
In case of COMException, you could use Marshal.GetExceptionForHR to convert
the HRESULT to an exception again. It will return correct message for it:
Imports Microsoft.Office.Interop.Outlook
Imports System.Runtime.InteropServices

Module Module1
Sub Main()
Dim app As New Application()
Dim inbox As MAPIFolder =
app.GetNamespace("MAPI").GetDefaultFolder(OlDefaul tFolders.olFolderInbox)
For Each item As MailItem In inbox.Items
Try
item.SaveAs("?.msg")
Catch e As System.Exception
If TypeOf e Is COMException Then
Dim ce As COMException = CType(e, COMException)
Dim e2 As System.Exception =
Marshal.GetExceptionForHR(ce.ErrorCode)
Console.Write(e2.Message)
Else
Console.Write(e.Message)
End If
End Try
Exit For
Next
End Sub

End Module
Hope this helps.

Regards,
Walter Wang (wa****@online.microsoft.com, remove ''online.'')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


这篇关于有关异常错误的更多详细信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆