RE>> [OJB-developers] Exceptions
Brought to you by:
thma
From: Domagoj J. <do...@la...> - 2002-05-07 12:05:50
|
<html> <br> Maybe you could use this example of code, it is code from enhydra 3.1 aplication server .<br> it helped me very much because I always see the root cause of exception.<br><br> But if you would use this , every exception in OJB should inherit from ChainedException.<br><br> And then when rethrowing you should write : <br><br> . . . . . .<br><br> } catch(BusinessException ex) {<br> throw new PresentationException("Error saving bussines object ! ", ex );<br> }<br> . . . . . . <br><br> <br> *************************************************CODE*****************************************************************************<br> //interface : <b> ChainedThrowable<br><br> </b>import java.io.PrintStream;<br> import java.io.PrintWriter;<br><br> public interface ChainedThrowable<br> {<br><br> public abstract Throwable getCause();<br><br> public abstract String getMessage();<br><br> public abstract void printStackTrace();<br><br> public abstract void printStackTrace(PrintStream printstream);<br><br> public abstract void printStackTrace(PrintWriter printwriter);<br> }<br><br> <br><br> <br> //class : <b> ChainedException<br><br> </b>import java.io.PrintStream;<br> import java.io.PrintWriter;<br><br> // Referenced classes of package com.lutris.util:<br> // ChainedThrowable, ChainedThrowableUtil<br><br> public class ChainedException extends Exception<br> implements ChainedThrowable<br> {<br><br> public ChainedException(String msg)<br> {<br> super(msg);<br> cause = null;<br> }<br><br> public ChainedException(String msg, Throwable cause)<br> {<br> super(msg);<br> this.cause = cause;<br> }<br><br> public ChainedException(Throwable cause)<br> {<br> super(ChainedThrowableUtil.makeMessage(cause));<br> this.cause = cause;<br> }<br><br> public Throwable getCause()<br> {<br> return cause;<br> }<br><br> public String getMessage()<br> {<br> return ChainedThrowableUtil.getMessage(this, super.getMessage());<br> }<br><br> public void printStackTrace()<br> {<br> super.printStackTrace();<br> ChainedThrowableUtil.printCauseTrace(this);<br> }<br><br> public void printStackTrace(PrintStream s)<br> {<br> super.printStackTrace(s);<br> ChainedThrowableUtil.printCauseTrace(this, s);<br> }<br><br> public void printStackTrace(PrintWriter s)<br> {<br> super.printStackTrace(s);<br> ChainedThrowableUtil.printCauseTrace(this, s);<br> }<br><br> private Throwable cause;<br> }<br><br> <br><br> //interface : <b> ChainedThrowableUtil<br><br> </b>import java.awt.print.PrinterIOException;<br> import java.io.*;<br> import java.lang.reflect.InvocationTargetException;<br> import java.rmi.RemoteException;<br> import java.rmi.activation.ActivationException;<br> import java.rmi.server.ServerCloneException;<br> import java.security.PrivilegedActionException;<br> import java.sql.SQLException;<br> import org.xml.sax.SAXException;<br><br> <br> class ChainedThrowableUtil<br> {<br><br> private ChainedThrowableUtil()<br> {<br> }<br><br> protected static String getMessage(ChainedThrowable except, String superMsg)<br> {<br> Throwable cause = except.getCause();<br> if(cause == null)<br> return superMsg;<br> String causeMsg = cause.getMessage();<br> if(causeMsg == null || causeMsg.length() == 0)<br> causeMsg = cause.getClass().getName();<br> return superMsg + ": " + causeMsg;<br> }<br><br> protected static String makeMessage(Throwable cause)<br> {<br> String causeMsg = cause.getMessage();<br> if(causeMsg == null)<br> return cause.getClass().getName();<br> else<br> return causeMsg;<br> }<br><br> protected static void printCauseTrace(ChainedThrowable except)<br> {<br> PrintWriter pw = new PrintWriter(System.err);<br> printChainedCauses(except.getCause(), pw);<br> pw.flush();<br> }<br><br> protected static void printCauseTrace(ChainedThrowable except, PrintStream s)<br> {<br> PrintWriter pw = new PrintWriter(s);<br> printChainedCauses(except.getCause(), pw);<br> pw.flush();<br> }<br><br> public static void printCauseTrace(ChainedThrowable except, PrintWriter out)<br> {<br> printChainedCauses(except.getCause(), out);<br> out.flush();<br> }<br><br> private static void printChainedCauses(Throwable cause, PrintWriter out)<br> {<br> if(cause != null)<br> {<br> out.println("*** Caused by:");<br> cause.printStackTrace(out);<br> if(!(cause instanceof ChainedThrowable))<br> if(cause instanceof PrinterIOException)<br> printChainedCauses(((Throwable) (((PrinterIOException)cause).getIOException())), out);<br> else<br> if(cause instanceof WriteAbortedException)<br> printChainedCauses(((Throwable) (((WriteAbortedException)cause).detail)), out);<br> else<br> if(cause instanceof ClassNotFoundException)<br> printChainedCauses(((ClassNotFoundException)cause).getException(), out);<br> else<br> if(cause instanceof ExceptionInInitializerError)<br> printChainedCauses(((ExceptionInInitializerError)cause).getException(), out);<br> else<br> if(cause instanceof InvocationTargetException)<br> printChainedCauses(((InvocationTargetException)cause).getTargetException(), out);<br> else<br> if(cause instanceof RemoteException)<br> printChainedCauses(((RemoteException)cause).detail, out);<br> else<br> if(cause instanceof ActivationException)<br> printChainedCauses(((ActivationException)cause).detail, out);<br> else<br> if(cause instanceof ServerCloneException)<br> printChainedCauses(((Throwable) (((ServerCloneException)cause).detail)), out);<br> else<br> if(cause instanceof PrivilegedActionException)<br> printChainedCauses(((Throwable) (((PrivilegedActionException)cause).getException())), out);<br> else<br> if(cause instanceof SQLException)<br> printChainedCauses(((Throwable) (((SQLException)cause).getNextException())), out);<br> else<br> if(cause instanceof SAXException)<br> printChainedCauses(((Throwable) (((SAXException)cause).getException())), out);<br> }<br> }<br> }<br><br> ******************************************************************************************************************************************************************</html> |