非托管异常如何在托管代码中捕获? [英] Unmanaged exception caught how in managed code?

查看:75
本文介绍了非托管异常如何在托管代码中捕获?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设以下内容:


//非托管代码

类UnmanagedException / *在非托管代码之外不可见* /

{

};

无效DoSomething()/ *可见(导出)到托管代码* /

{

抛出新的UnmangedException();

}


//托管代码

//调用非托管代码

试试

{

DoSomething();

}

catch(???)

{

}

托管代码中适合的类型是什么?


我看到SEHException及其父ExternalException,但这些(自动)

包装似乎是基于COM异构类型的异常。


我的理解是托管框架自动捕获,

重新包装,并重新抛出非托管异常,但是什么呢?


谢谢


-

Bret Pehrson

mailto:br ** @ infowest.com
NOSPAM - 在所有电子邮件通信中包含此密钥<< 38952rglkwdsl>>

Suppose the following:

// Unmanaged code
class UnmanagedException /* not visible outside of unmanaged code */
{
};
void DoSomething() /* visible (exported) to managed code */
{
throw new UnmangedException();
}

// Managed code
// Call into unmanaged code
try
{
DoSomething();
}
catch (???)
{
}
What is/are suitable types to catch in the managed code?

I see SEHException and its parent ExternalException, but these (automatic)
wrappers seem to be based on COM hresult types of exceptions.

My understanding is that the managed framework automatically catches,
repackages, and rethrows the unmanaged exception, but as what?

Thanks

--
Bret Pehrson
mailto:br**@infowest.com
NOSPAM - Include this key in all e-mail correspondence <<38952rglkwdsl>>

推荐答案

Hello Bret,


感谢您在小组中发帖。


根据描述,问题是:如何捕获无人类型
托管代码中的
异常?如果我误解了这个问题,请随时在这里发帖。


实际上,我们可以捕获公共语言运行时异常

(System :: Exception)和Managed中的非托管异常(C ++ struct)


#using< mscorlib.dll>

使用命名空间系统;


struct S {

};


void foo(int i){

if(i == 0){

throw(新S());

}

else {

Exception * e = new Exception;

扔e;

}

}


int main(){

int nRes = 1;

for(int i = 0; i< 2; i ++){

try {

foo(i);

}

catch(S * s){

(s);

控制台:: WriteLine(S"抓到一个非托管异常!);

nRes - = 2 * i;

}

catch(异常) * e){

Console :: WriteLine(S" Caught a managed exception:{0}",e-> ToString());

nRes - =我;

}

}


返回nRes;

}


请注意,两个catch语句都保护相同的try块

单独捕获托管和非托管异常。另请注意

此示例生成的代码是完全托管代码。


您还可以参考
http://msdn.microsoft.com/library/en...edsampledemons

tratesthrowingcatchingmanagedunmanagedexceptionswi thinsingleprocess.asp?fram

e = true此示例的详细信息。


这是否回答了您的问题好的问候,

Yanhong Huang

微软社区支持


获取安全! - www.microsoft.com/security

发布是按原样提供的。没有保证,也没有授予任何权利。

Hello Bret,

Thanks for posting in the group.

Based on the description, the question is: How to trap a unmanged type
exception in managed code? Please feel free to post here if I misunderstood
the question.

In fact, we can catch the common language runtime exception
(System::Exception) and the unmanaged exception (C++ struct) in Managed
Extensions for C++ code. Please refer to the following sample code:

#using <mscorlib.dll>
using namespace System;

struct S {
};

void foo(int i) {
if (i == 0) {
throw (new S());
}
else {
Exception *e = new Exception;
throw e;
}
}

int main() {
int nRes = 1;
for (int i = 0; i < 2; i++) {
try {
foo(i);
}
catch (S* s) {
(s);
Console::WriteLine(S"Caught an unmanaged exception!");
nRes -= 2*i;
}
catch (Exception* e) {
Console::WriteLine(S"Caught a managed exception: {0}", e->ToString());
nRes -= i;
}
}

return nRes;
}

Note that the same try block is guarded by both of the catch statements
that catch managed and unmanaged exceptions individually. Also note that
the code generated by this sample is completely managed code.

You can also refer to
http://msdn.microsoft.com/library/en...edsampledemons
tratesthrowingcatchingmanagedunmanagedexceptionswi thinsingleprocess.asp?fram
e=true for the details on this sample.

Does that answer your question?

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.


你好Bret,

你可以在c#中捕获非托管异常,但你可以'不要用它们。你只能

重新抛出它们


如果你有这样的尝试/捕获块

试试

{

....

}

catch

{

。 ..

throw;

}


catch块将捕获所有异常(托管和非托管)和

* thow *运算符将重新抛出捕获的异常*原样*。


这是使用try / catch块捕获非托管异常的唯一方法

in c#你可以选择吞下它或重新抛出它。


-

B \ rgds

100

" Bret Pehrson" <峰; br ** @ infowest.com>在消息中写道

news:40 *************** @ infowest.com ...
Hi Bret,
You can catch unmanaged exception in c# but you can''t use them. you can only
rethrow them

if you have try/catch block like this
try
{
....
}
catch
{
...
throw;
}

the catch block will catch all exceptions (managed and unmanaged) and the
*thow* operator will rethrow catched exception *AS IS*.

This is the only way you can catch unmanaged exception with try/catch block
in c# and your choices are either to swallow it or rethrow it.

--
B\rgds
100
"Bret Pehrson" <br**@infowest.com> wrote in message
news:40***************@infowest.com...
假设如下:

//非托管代码
类UnmanagedException / *在非托管代码之外不可见* /
{
};
void DoSomething()/ * visible(导出) )托管代码* /
{抛出新的UnmangedException();
}
//托管代码
//调用非托管代码
尝试
{
DoSomething();
}
赶上(???)
{
}

什么是否是托管代码中的合适类型?

我看到SEHException及其父ExternalException,但这些(自动)
包装器似乎基于COM异构类型的异常。 br />
我的理解是托管框架会自动捕获,重新打包,并重新抛出非托管异常,但是什么呢?

谢谢

-
Bret Pehrson
mailto:br ** @ infowest.com
NOSPAM - 包含此密钥所有的电子邮件信件<< 38952rglkwdsl>>
Suppose the following:

// Unmanaged code
class UnmanagedException /* not visible outside of unmanaged code */
{
};
void DoSomething() /* visible (exported) to managed code */
{
throw new UnmangedException();
}

// Managed code
// Call into unmanaged code
try
{
DoSomething();
}
catch (???)
{
}
What is/are suitable types to catch in the managed code?

I see SEHException and its parent ExternalException, but these (automatic)
wrappers seem to be based on COM hresult types of exceptions.

My understanding is that the managed framework automatically catches,
repackages, and rethrows the unmanaged exception, but as what?

Thanks

--
Bret Pehrson
mailto:br**@infowest.com
NOSPAM - Include this key in all e-mail correspondence <<38952rglkwdsl>>



我知道这真是一个挑剔的问题,但我还是得到了

来掌握MC ++的规则和注册:


//> throw(new S());

throw S(); //我们需要从freestore分配吗?


//> catch(S * s){

catch(S& s)//通过引用捕获它是否更可取?


//> (一个或多个); //这是做什么的?


您提到所有生成的代码都是托管的。这个案件需要''新S()'

吗?


谢谢。

Arnold the Aardvark
http://www.codeproject.com/cpp/garbage_collect。 asp

I know this is really a nit-picky question, but I''m still getting
to grips with MC++''s rules and regs:

//> throw (new S());
throw S(); // Do we need to allocate from the freestore?

//> catch (S* s) {
catch (S& s) // Isn''t it preferable to catch by reference?

//> (s); // What does this do?

You mention that all the resulting code is managed. Is the ''new S()''
required to make this the case?

Thanks.
Arnold the Aardvark
http://www.codeproject.com/cpp/garbage_collect.asp


这篇关于非托管异常如何在托管代码中捕获?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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