• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get Exception Name?

 
James Dekker
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get the name of the runtime type of any object with getClass().getName()
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim!

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

-JD
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic