File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Get Exception Name?

James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 221
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:




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,

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

Joined: Jan 30, 2000
Posts: 18671

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: 2398
You can get the name of the runtime type of any object with getClass().getName()
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46345
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: 221
Thanks Jim!

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

I agree. Here's the link:
subject: Get Exception Name?
It's not a secret anymore!