把错误信息 组合成一个字符串作为构件一个异常( 在NEW的时候给构造方法)的参数就可以了

解决方案 »

  1.   

    很简单,给你一个简单的例子
    1 定义异常类
    public class TestException extends Exception {
    private String e;
    public TestException(String e){
    this.e = e;
    }
    public String toString(){
            return "抛出异常"+e;
        }
    }
    2 测试类
    public class Test {
    public static void main(String[] args) { 
    String yourString="xxxxx";//这里是你说的错误信息、错误代码、函数名、文件名等等信息,要自己写的
    try{
             test(yourString);//执行普通函数
            }catch(TestException e){
             System.out.println(e);
            }
    }
    //普通函数,在此处跑出自定义的异常
    public static void test(String yourString) throws TestException{
    throw new TestException(yourString);
    }
    }
      

  2.   

    楼主是否要自己来格式化异常栈内容呢?如果是的话请参考一下代码。可能与你说的思路不同。Throwable t = 你捕获到的异常。
    while(t!=null){
    out.print("<font color='red'>");
    out.print(t.getClass().getName()+ ":"+t.getMessage()+"<br/>");
    out.print("</font>");
    StackTraceElement[] elements = t.getStackTrace();
    for(int i=0;i<elements.length;i++){
    StackTraceElement e = elements[i];
    out.print("&nbsp;&nbsp;&nbsp;"+e.getClassName()+"#");
        out.print(e.getMethodName()+"");
        out.print("(line:"+e.getLineNumber()+")");
        out.print("<br/>");
    }
    t = t.getCause();
    }
      

  3.   

    格式化异常栈是要用,但我在Action中写了
    Action里面
            try{
                .......
            }catch(TestException e){//出错了
                ..........
            }
    Action类里面
    public static void Action() throws TestException{
            throw new TestException("......“);
        }
    却在我表明的出错了地方放报错了