Find the type of the exception

  • Thread starter Thread starter greg
  • Start date Start date
G

greg

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
 
You can do the following

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

}


- José
 
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.
 
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."
 
"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
 
i found it in msdn forum .. try this ..
catch (Exception exception)
{
Trace.WriteLine(exception.GetType().FullName);
}
 
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.
 
Peter Bromberg said:
"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
 
(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





- 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?
 
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
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top