Class HighLite Throwable

The first thing a programmer learns about exceptions is how to ignore them. Then a programmer learns that it is always best to throw them up the stack to the caller. And if you are the caller you can always print the stack trace to get a glimpse of what line of code is possibly at fault. The Throwable class is the base class for all exceptions, and errors for that matter. If you ever had to do anything interesting with Java you must had caught or thrown an exception.

The two methods from the Throwable class that most developers are familiar with are the printStackTrace and getMessage. The information that is printed out when you call printStackTrace is also available to you with the getStrackTrace method. The getStrackTrace method returns an array of StackTraceElement objects. The StackTraceElement class has getter methods for the class name, method name, file name, and line number to access the information of the call stack trace.

With the information made available to you by the Throwable class you see the caller hierarchy. You can trace the callers all the way back to the main method of the class that launched an application. You have access to this information by just creating a Throwable object, no need to throw it.

Throwable t = new Throwable();
StackTraceElement[] stack = t.getStackTrace();

With this information you can allow or disallow certain call hierarchy or certain classes from calling a method a specific method. You can print the line number and class name in a debugging statement, just what Log4j does. The Throwable class provides access to metadata of your class environment at runtime. A note of warning, creating a Throwable seems like a very expensive call so make sure you know what you are doing.

Related posts:

  1. Class HighLite File
  2. Class HighLite GraphicsEnvironment
  3. Class HighLite Calendar
  4. Class HighLite InetAddress
  5. Class HighLite Collections
  6. Class HighLite ThreadLocal

Leave a Reply