当前位置:编程学习 > JAVA >>

异常框架-异常链和log4j

package test;  

/**
*  
* @author jenhy
*
*/

class HighLevelException extends Exception{


HighLevelException(Exception e){
   super(e);
}
}
class MiddleLevelException extends Exception{

MiddleLevelException(Exception e){
   super(e);
}
}
class LowLevelException extends Exception{

LowLevelException(){
   super();
}

LowLevelException(Exception e){
   super(e);
}
}

public class ExceptionTest {  

    /**
     *  
     * @throws HighLevelException
     */
    public void highLevelAccess() throws HighLevelException {  
        try {  
            middleLevelAccess();  
        } catch (Exception e) {  
            throw new HighLevelException(e);  
        }  
    }  

    /**
     *  
     * @throws MiddleLevelException
     */
    public void middleLevelAccess() throws MiddleLevelException {  
        try {  
            lowLevelAccess();  
        } catch (Exception e) {  
            throw new MiddleLevelException(e);  
        }  
    }  

    /**
     *  
     * @throws LowLevelException
     */
    public void lowLevelAccess() throws LowLevelException {  
        throw new LowLevelException();  
    }  

    /**
     *  
     * @param args
     */
    public static void main(String[] args) {  
        try {  
            new ExceptionTest().highLevelAccess();  
        } catch (HighLevelException e) {  
            Throwable cause = e;  
            for (;;) {  
                if (cause == null)  
                    break;  

                //打印Cause by  
                System.out.println("Caused by: " + cause.getClass().getName() + ":" + cause.getMessage());  

                //打印堆栈  
                StackTraceElement[] ste = cause.getStackTrace();  
                for (int i = 0; i < ste.length; i++) {  
                    System.out.println("ClassName" + i + ":" + ste[i].getClassName() + "\nMethodName:" + ste[i].getMethodName() + "\nFileName:" + ste[i].getMethodName() + "\nLineNumber:" + ste[i].getLineNumber());  
                    System.out.println();  
                }  

                //递归  
                cause = cause.getCause();  
            }  
        }  
    }  

}
 
当程序捕获到了一个底层异常le,在处理部分选择了继续抛出一个更高级别的新异常给此方法的调用者。这样异常的原因就会逐层传递。这样,位于高层的异常递 归调用getCause()方法,就可以遍历各层的异常原因。这就是Java异常链的原理。异常链的实际应用很少,发生异常时候逐层上抛不是个好注意,上 层拿到这些异常又能奈之何?而且异常逐层上抛会消耗大量资源,因为要保存一个完整的异常链信息。
 
实践中的异常框架:
 
package cn.java.exception;
/**
 *
 * @author jenhy
 *
 */
public class ExceptionTest {
 /**
  *
  * @throws Exception
  */
 public void highLevelAccess() throws Exception {
  try {
   middleLevelAccess();
  } catch (Exception e) {
//   throw new HighLevelException(e);
   throw e;
  }
 }
 /**
  *
  * @throws MiddleLevelException
  */
 public void middleLevelAccess() throws Exception {
  try {
   lowLevelAccess();
  } catch (Exception e) {
//   throw new MiddleLevelException(e);
   throw e;
  }
 }
 /**
  *
  * @throws LowLevelException
  */
 public void lowLevelAccess() throws Exception {
//  throw new LowLevelException();
  throw new RuntimeException();
 }
 /**
  *
  * @param args
  */
 public static void main(String[] args) {
  try {
   new ExceptionTest().highLevelAccess();
  } catch (Exception e) {
   Throwable cause = e;
   for (;;) {
    if (cause == null)
     break;
    //打印Cause by
    System.out.println("Caused by: " + cause.getClass().getName() + ":" + cause.getMessage());
    //打印堆栈
    StackTraceElement[] ste = cause.getStackTrace();
    for (int i = 0; i < ste.length; i++) {
     System.out.println("ClassName" + i + ":" + ste[i].getClassName() + "/nMethodName:" + ste[i].getMethodName() + "/nFileName

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,