Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Casting caught Exception to Real Type

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to catch Exception, cast it the Object Type, lets say ArrayOutOfBoundsException (using getClass() perhaps) and then get the message from that object?

A generic Error / Exception Handler if you will.
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can call getMessage() without casting it to another class; you'll get the appropriate message. If you mean you want to create a message based on the class, then you certainly could use getClass().getName() to get the name of the Exception class.
 
Christopher McCauley
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that everytime I try that, the message returned is '
null' ?.?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, some exceptions thrown by the JVM itself traditionally have null messages. This is common for NullPointerExceptions, ArrayIndexOutOfBoundsExceptions, etc.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could always do what one of my long-gone co-workers has done:



Yes, it's true.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heh - that's pretty horrid. Though I would point out for Christopher's benefit that if you're trying to extract some sort of String description of an exception, toString() is often a better choice than getMessage() - at least for a "general" catch block that catches something like all Exceptions or all RuntimeExceptions. The toString() method usually gives you the exception class name followed by the message (if that message is not null). In many cases the exception name is the error message (or an important part of it), and so you can't assume getMessage() by itself will be sufficient to give you useful information. Unless you've caught a specific type of exception which is known to have reliable messages (e.g. one you wrote yourself, or have tested enough to trust).
 
We begin by testing your absorbancy by exposing you to this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic