Determine Object Type

  • Thread starter Thread starter Jeff S
  • Start date Start date
J

Jeff S

I want to pass Exception objects of many types (e.g., SqlException,
Excpetion, MyCustomxception) to a routine. The routine receives each as type
object. How do I test 'object' to determine which type of Exception was
passed in to the receiving method?

Thanks in advance!
 
You can also use the 'is' keyword. Like this:

private void CheckException(Exception e)
{
if (e is FormatException) ...
else if (e is ArgumentExcetion) ...
else if (e is MyCustomException) ...
}

- Noah Coad -
Microsoft MVP
 
Hi Jeff

Just to add to what Noad said, using 'is' is better since it would not result in an exception. In the former case i mentioned, an exception may result if the object is not initialized and you use the GetType method. :

Regards
fbhca
 
GetType() will also test for the exact type, whereas "is" will also return
true for descendants of the class you are checking for.


--
Pete
-------
http://www.DroopyEyes.com
Audio compression components, DIB Controls, FastStrings

http://www.HowToDoThings.com
Read or write articles on just about anything

fbhcah said:
Hi Jeff,

Just to add to what Noad said, using 'is' is better since it would not
result in an exception. In the former case i mentioned, an exception may
result if the object is not initialized and you use the GetType method. :)
 
Excatly...

Just to add what Pete mentioned, the reason why 'is' returns true for
descendents is that, 'is' checks whether an object is 'castable' to the
required type. Since derived types are castable to their base types, it
will return true.

HTH,
fbhcah
 
Forgot to mention this:
The reason why 'is' won't result in an exception is that, 'is' will also check whether the object is null or not too.

To summarize,
'is' will check whether the object is null,
and will also check whether it's castable to the required type or not.

HTH,
fbhcah
 
Now Bill Clinton can know what the meaning of 'is' is.

har har har!



fbhcah said:
Forgot to mention this:
The reason why 'is' won't result in an exception is that, 'is' will also
check whether the object is null or not too.
 
<lol> Now that is funny... :)


Jeff S said:
Now Bill Clinton can know what the meaning of 'is' is.

har har har!




check whether the object is null or not too.
 
The 'e' (Exception type) object when passed to another routine will
expose .Message and .InnerException, which could be used to obtain the
type of exception thrown.

Try
Catch e as Exception
myMeth(e)
End Try

Public Sub myMeth(ee as Exception)
MessageBox.Show(e.InnerException & e.Message)
End Sub


with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Back
Top