如何自定义写自定义异常类 把错误信息 组合成一个字符串作为构件一个异常( 在NEW的时候给构造方法)的参数就可以了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 很简单,给你一个简单的例子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); }} 楼主是否要自己来格式化异常栈内容呢?如果是的话请参考一下代码。可能与你说的思路不同。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(" "+e.getClassName()+"#"); out.print(e.getMethodName()+""); out.print("(line:"+e.getLineNumber()+")"); out.print("<br/>"); } t = t.getCause();} 格式化异常栈是要用,但我在Action中写了Action里面 try{ ....... }catch(TestException e){//出错了 .......... }Action类里面public static void Action() throws TestException{ throw new TestException("......“); }却在我表明的出错了地方放报错了 ssh ajax 检查是否用户名重复 如何衡量一个java程序员水平? 如何从JAVA SE转到JAVA EE? 大家做EE开发时,包组织结构一般是什么? 我该怎么做,您给点意见吧。 spring getBean 的问题 ******** 请问STRUTS问题 artdialog遮罩层在内容层的下面一层,因而无法屏蔽用户操作 tomcat的web.xml中关于mime类型配置问题 在jboss+tomcat中,如何用jsp调用另外一台机器上部署的ejb? Tomcat 问题 出现404 错误 求助:数据库中字符串读取不完全
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);
}
}
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(" "+e.getClassName()+"#");
out.print(e.getMethodName()+"");
out.print("(line:"+e.getLineNumber()+")");
out.print("<br/>");
}
t = t.getCause();
}
Action里面
try{
.......
}catch(TestException e){//出错了
..........
}
Action类里面
public static void Action() throws TestException{
throw new TestException("......“);
}
却在我表明的出错了地方放报错了