473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

non-clr exceptions

Hi,

How do you catch an exception from unmanaged code?

Will

try
{
// unmanaged code
}
catch (Exception e)
{
}

do?

Or do I need:

try
{
}
catch (Exception e)
{
// for managed exceptions
}
catch
{
// for unmanaged exceptions
}

Which exceptions will go to the first catch block and
which will go to the second?

Jon Paugh
Nov 22 '05 #1
2 1667
The first block will catch any managed exceptions (ie those derived from
Exception), and the second one will catch any other exceptions, though
there's no way to get to the value that was thrown.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Jon Paugh" <an*******@discussions.microsoft.com> wrote in message
news:40****************************@phx.gbl...
Hi,

How do you catch an exception from unmanaged code?

Will

try
{
// unmanaged code
}
catch (Exception e)
{
}

do?

Or do I need:

try
{
}
catch (Exception e)
{
// for managed exceptions
}
catch
{
// for unmanaged exceptions
}

Which exceptions will go to the first catch block and
which will go to the second?

Jon Paugh

Nov 22 '05 #2
Typically unmanaged code will throw an exception of type SEHException or
COMException, so if you use

catch(System.Exception ex){}

you will catch both of these since they are both derived from
System.Exception. However, figuring out what the exception means may be
difficult as the mapping from unmanaged exceptions to managed exception
types is not simple or direct, and some exceptions may not roundtrip through
the CLR faithfully.

Using the untyped catch block

catch { ... }

will catch untyped, or non-CLI compliant exceptions. This will catch
exceptions of any type, including untyped exceptions (e.g. numeric values,
such as 0xC0000005). If all your code is managed and CLI-compliant then you
should never get any untyped exceptions as the runtime will translate Win32
exception codes into managed exception types for you. Also, using this form
of a catch block means that you will not have an exception object that you
can programmatically examine.

If you are writing managed code I would not use this - let untyped
exceptions bubble up the callstack. I would instead use

catch(Exception) { ... } // no object - don't care about it

or
catch(Exception ex) {...} ///if you want to use the object.

"Jon Paugh" <an*******@discussions.microsoft.com> wrote in message
news:40****************************@phx.gbl...
Hi,

How do you catch an exception from unmanaged code?

Will

try
{
// unmanaged code
}
catch (Exception e)
{
}

do?

Or do I need:

try
{
}
catch (Exception e)
{
// for managed exceptions
}
catch
{
// for unmanaged exceptions
}

Which exceptions will go to the first catch block and
which will go to the second?

Jon Paugh

Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
14
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.