aspose file tools*
The moose likes Beginning Java and the fly likes Get Exception Name? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Get Exception Name?" Watch "Get Exception Name?" New topic
Author

Get Exception Name?

James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 215
Suppose my program throws the following exception:



Now, suppose I try to catch this by doing this:



How to I extract the exception name (in this case: java.lang.ArrayIndexOutOfBoundsException) out of t?

Looked at the API and found the following methods:

-Throwable.getCause();

-Throwable.getLocalizedMessage();

-Throwable.toString();

Tried these and none of these just gave me the name of the exception...

Is there anyway I can just extract the fully qualified (along with package name) Exception name and thats it?

Thank you,

JD
[ May 08, 2007: Message edited by: James Dekker ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
t.getClass().getName()

In JDK 5 and later, you can also use t.getClass().getSimpleName() if you want to omit the package from the name. Or t.getClass().getPackage().getName() to get the name of the package, without the class.


"I'm not back." - Bill Harding, Twister
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You can get the name of the runtime type of any object with getClass().getName()
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
It's nice to know the name of an Exception you are getting . . .

But it's a lot nicer to find out why an ArrayOutOfBoundException is occurring and stop it happening in the first place.
James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 215
Thanks Jim!

The call to getSimpleName() is exactly what I was searching for!

-JD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get Exception Name?