JDK and exception handling


If you are modeling the UpdateException as a unchecked exception, you will have to extend from RuntimeException . In addition if you are using JDK1.3.x and lower, you will also provide the wrapped exception attribute in your own exception. JDK1.4 onwards, you can wrap the ‚“causative exception ‚½ in the parent class RuntimeException as a java.lang.Throwable attribute thus allowing you to carry around the ‚“causative exception ‚½. For e.g. SQLException is the ‚“causative exception ‚½ in Listing 9.3. In the Throwable class there is a new method getCause to get the cause of any exception which returns the wrapped exception if exists. This can result in an exception chain since the cause itself can have a cause. Prior to 1.4 Exception classes had their own non-standard exception chaining mechanisms. For instance, RemoteException was used to carry the actual exception across different JVMs or from EJB tier to web tier . As of 1.4, all of these classes have been retrofitted to use the standard exception chaining mechanism.

Additional exception handling features in JDK1.4 include programmatic access to stack trace. This is a boon for real time error monitoring and alert facilities. Often these systems need to manually parse the stack dump for keywords. This is been made much easier. One can invoke getStackTrace method on the Exception (or Throwable ) and get an array of StackTraceElement s returned. Each StackTraceElement provides the following methods .

  • getClassName

  • getFileName

  • getLineNumber

  • getMethodName

  • isNativeMethod

By calling the above methods, you can display the stack trace in any format you like. In addition, elegant error monitoring systems can be written. For instance, the error monitoring system should alert the appropriate support team for the sub system by intelligently analyzing the stack trace. This has been made easier. The following code snippet can be used with JDK 1.4

 StackTraceElement elements[] = e.getStackTrace(); for (int i=0, n=elements.length; i<n; i++) {    if (elements[i].getClassName.equals("LegacyAccessEJB)       && elements[i].getMethodName().equals(invokeCOBOL)    {       //Alert the COBOL support team    } } 

This code snippet checks if the exception originated in LegacyAccessEJB during invoking a method named ‚“ invokeCOBOL ‚½, it will alert the COBOL support team. Obviously the decision tree is not as simple as shown, but at least it removes the headache of parsing the trace for the same information.




Struts Survival Guide. Basics to Best Practices
Struts Survival Guide: Basics to Best Practices (J2ee Survival Series)
ISBN: 0974848808
EAN: 2147483647
Year: 2004
Pages: 96

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net