如何 把 printStackTrace转换成字符串
展开全部
建议自定义一个总的Exception
Java code public abstract class GoodsException extends Exception{ /** * */ private static final long serialVersionUID = 1L; public GoodsException(){ super(); } public GoodsException(String message){ super(message); } public GoodsException(Throwable cause){ super(cause); } public GoodsException(String message, Throwable cause){ super(message, cause); } }
在继承定义详细Exception
Java code public class DpulicationGoodsException extends GoodsException { /** * */ private static final long serialVersionUID = 1L; public DpulicationGoodsException(){ super(); } public DpulicationGoodsException(String message){ super(message); } public DpulicationGoodsException(Throwable cause){ super(cause); } public DpulicationGoodsException(String message, Throwable cause){ super(message, cause); } }
利用多态捕获总的catch(GoodsException e){
err.println(e.getMessage());
}
Java code public abstract class GoodsException extends Exception{ /** * */ private static final long serialVersionUID = 1L; public GoodsException(){ super(); } public GoodsException(String message){ super(message); } public GoodsException(Throwable cause){ super(cause); } public GoodsException(String message, Throwable cause){ super(message, cause); } }
在继承定义详细Exception
Java code public class DpulicationGoodsException extends GoodsException { /** * */ private static final long serialVersionUID = 1L; public DpulicationGoodsException(){ super(); } public DpulicationGoodsException(String message){ super(message); } public DpulicationGoodsException(Throwable cause){ super(cause); } public DpulicationGoodsException(String message, Throwable cause){ super(message, cause); } }
利用多态捕获总的catch(GoodsException e){
err.println(e.getMessage());
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public static void main(String[] args) {
try {
String str = "sss";
Integer.valueOf(str);
} catch (Exception ex) {
<span style="color: #FF0000;">//是否是少了这行</span>
System.out.println(ex.getLocalizedMessage());
StackTraceElement[] se = ex.getStackTrace();
for (int i = 0; i < se.length; i++) {
System.out.println(se[i].toString());
}
}
try {
String str = "sss";
Integer.valueOf(str);
} catch (Exception ex) {
<span style="color: #FF0000;">//是否是少了这行</span>
System.out.println(ex.getLocalizedMessage());
StackTraceElement[] se = ex.getStackTrace();
for (int i = 0; i < se.length; i++) {
System.out.println(se[i].toString());
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询