473,396 Members | 1,859 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,396 software developers and data experts.

Find the type of the exception

I have been struggling with the following issue:

How can one find the type of the exception? In other words I would
likes to come up with piece of code that does the following (written
in pseudo code):

if (exception.Type() == outOfMemoryException)
then
......

Note that I can't use catch statement in this case.

Suggestions will be highly appreciated.

Thanks,
Greg

Jun 8 '07 #1
11 6908

U can use "is" in such cases:

if (exception is OutOfMemoryException)
{ }

Alex
http://devkids.blogspot.com
I have been struggling with the following issue:

How can one find the type of the exception? In other words I would
likes to come up with piece of code that does the following (written
in pseudo code):

if (exception.Type() == outOfMemoryException)
then
.....
Note that I can't use catch statement in this case.

Suggestions will be highly appreciated.

Thanks,
Greg

Jun 8 '07 #2
On Jun 9, 12:07 am, greg <greg.johnse...@gmail.comwrote:
I have been struggling with the following issue:

How can one find the type of the exception? In other words I would
likes to come up with piece of code that does the following (written
in pseudo code):

if (exception.Type() == outOfMemoryException)
then
.....

Note that I can't use catch statement in this case.

Suggestions will be highly appreciated.

Thanks,
Greg
I don't think it's possible to get the type of excepion without using
the catch block. In this case code just breaks saying unhandled
exception has occured.

Jun 8 '07 #3
You can do the following

Catch (Exception ex)
{
if (ex is outOfMemoryException)
{
// do what I need
}
else
{
throw;
}

}
- José
"greg" <gr************@gmail.coma écrit dans le message de news:
11**********************@g37g2000prf.googlegroups. com...
>I have been struggling with the following issue:

How can one find the type of the exception? In other words I would
likes to come up with piece of code that does the following (written
in pseudo code):

if (exception.Type() == outOfMemoryException)
then
.....

Note that I can't use catch statement in this case.

Suggestions will be highly appreciated.

Thanks,
Greg

Jun 8 '07 #4
On Jun 9, 12:26 am, "Aneesh Pulukkul[MCSD.Net]" <anees...@gmail.com>
wrote:
On Jun 9, 12:07 am, greg <greg.johnse...@gmail.comwrote:


I have been struggling with the following issue:
How can one find the type of the exception? In other words I would
likes to come up with piece of code that does the following (written
in pseudo code):
if (exception.Type() == outOfMemoryException)
then
.....
Note that I can't use catch statement in this case.
Suggestions will be highly appreciated.
Thanks,
Greg

I don't think it's possible to get the type of excepion without using
the catch block. In this case code just breaks saying unhandled
exception has occured.- Hide quoted text -

- Show quoted text -
Hey Alex, can we do it without using catch block? greg needs this
requirement it seems - "Note that I can't use catch statement in this
case."

Jun 8 '07 #5
"exception" here is an instance of a class derived from System.Exception.

So, how are you going to check it's type if you cannot catch it, as that's
how you gain access to a live exception instance? Where did the rule that
you cannot use "Catch" come from?
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"greg" wrote:
I have been struggling with the following issue:

How can one find the type of the exception? In other words I would
likes to come up with piece of code that does the following (written
in pseudo code):

if (exception.Type() == outOfMemoryException)
then
......

Note that I can't use catch statement in this case.

Suggestions will be highly appreciated.

Thanks,
Greg

Jun 8 '07 #6
i found it in msdn forum .. try this ..
catch (Exception exception)
{
Trace.WriteLine(exception.GetType().FullName);
}


Jun 8 '07 #7
On Jun 8, 1:11 pm, Peter Bromberg [C# MVP]
<pbromb...@yahoo.yabbadabbadoo.comwrote:
So, how are you going to check it's type if you cannot catch it, as that's
how you gain access to a live exception instance? Where did the rule that
you cannot use "Catch" come from?
Peter
I am using an ugly hack - unhandled exception event handler - because
I am using threads and they cause trouble. There must be a more
elegant approach but I don't have time and I don't need this program
to be pretty, just to work.

Thanks to all of you guys! Using "is" works! You are awesome.
Jun 8 '07 #8

"Peter Bromberg [C# MVP]" <pb*******@yahoo.yabbadabbadoo.comwrote in
message news:E2**********************************@microsof t.com...
"exception" here is an instance of a class derived from System.Exception.

So, how are you going to check it's type if you cannot catch it, as that's
how you gain access to a live exception instance? Where did the rule that
you cannot use "Catch" come from?
(1) you can "new Exception()" without throwing
(2) someone else might have caught the exception, and handed it to you for
further processing

In any case the "is", "as", and "typeof" keywords can solve the problem
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"greg" wrote:
>I have been struggling with the following issue:

How can one find the type of the exception? In other words I would
likes to come up with piece of code that does the following (written
in pseudo code):

if (exception.Type() == outOfMemoryException)
then
......

Note that I can't use catch statement in this case.

Suggestions will be highly appreciated.

Thanks,
Greg


Jun 8 '07 #9
On Jun 9, 2:49 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
"Peter Bromberg [C# MVP]" <pbromb...@yahoo.yabbadabbadoo.comwrote in
messagenews:E2**********************************@m icrosoft.com...
"exception" here is an instance of a class derived from System.Exception.
So, how are you going to check it's type if you cannot catch it, as that's
how you gain access to a live exception instance? Where did the rule that
you cannot use "Catch" come from?

(1) you can "new Exception()" without throwing
(2) someone else might have caught the exception, and handed it to you for
further processing

In any case the "is", "as", and "typeof" keywords can solve the problem
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net
"greg" wrote:
I have been struggling with the following issue:
How can one find the type of the exception? In other words I would
likes to come up with piece of code that does the following (written
in pseudo code):
if (exception.Type() == outOfMemoryException)
then
......
Note that I can't use catch statement in this case.
Suggestions will be highly appreciated.
Thanks,
Greg- Hide quoted text -

- Show quoted text -
Okay you are using the UnhandledExceptionEventHandler delegate to get
the unhandled exception. So the UnhandledExceptionEventArgs would give
you the ExceptionObject it seems.Here you can use "is", "as", and
"typeof", right?

Jun 9 '07 #10
Okay you are using the UnhandledExceptionEventHandler delegate to get
the unhandled exception. So the UnhandledExceptionEventArgs would give
you the ExceptionObject it seems.Here you can use "is", "as", and
"typeof", right?
Yes, but first you should cast the Object to Exception, and then we
can use "is", as was suggested above, to check what the exception type
is.

Cheers and thanks,
Greg

Jun 9 '07 #11
On Jun 9, 11:43 am, greg <greg.johnse...@gmail.comwrote:
Okay you are using the UnhandledExceptionEventHandler delegate to get
the unhandled exception. So the UnhandledExceptionEventArgs would give
you the ExceptionObject it seems.Here you can use "is", "as", and
"typeof", right?

Yes, but first you should cast the Object to Exception, and then we
can use "is", as was suggested above, to check what the exception type
is.

Cheers and thanks,
Greg
Exactly. First cast to Exception object, then check the type of
exception.

Jun 9 '07 #12

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

Similar topics

11
by: Christopher J. Bottaro | last post by:
I actually want all the parent classes too. So if D derives off C derives off B derives off A, I ultimately want a tuple ('D', 'C', 'B', 'A'). For those of you following the Python Documentation...
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
3
by: Professor Frink | last post by:
First off, I apologize if this gets long. I'm simply trying to give you all enough information to help me out. I'm writing (almost finished, actually), my first VB.Net application. It's a forms...
0
by: Sam Fields | last post by:
I have found very little regarding the error "Unable to find an entry point named EnumerateSecurityPackagesW in DLL security.dll. ". I have an ASP.NET Web Service being accessed via SSL. I found...
1
by: TRI_CODER | last post by:
I am trying to solve the following exception. The exception occurs when my ASP.NET code behind code attemtps to access a remore site using SSL. Please note that all certificates are valid and the...
4
by: Zytan | last post by:
I deal with a bunch of exception handlers by calling a function that writes the info to a log file. Of course, this takes in Exception, so I can pass it any exception class. But, it'd be great if...
1
by: OrionLee | last post by:
I am using C# to work with a 3rd party DLL (Nevron Charts), and attempting to serialise it. The serialisation itself is handled somewhere inside the DLL, so to get it to happen you call the Nevron's...
9
by: JoeP | last post by:
Hi All, How can I find the reason for such an error: Failure sending mail. Some Code... oMailMessage.IsBodyHtml = False oMailMessage.Body = cEmailBody Dim oSMTP As New SmtpClient...
5
by: Ohad weiss | last post by:
Hi all, I have a class in project A. At the same solution, I have another project - named project B. I want to write a piece of code in that class from project B that finds the class in...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.