当前位置: 网站首页 > JAVA > j2se

轻松查找所需异常信息的源代码

时间:1970-1-1 08:33:31来源: j2se作者:admin 点击:0次 字体 [ С]

每次出现异常信息时,在后台窗口找异常信息都是一件很烦的事,而且当打印的异常信息比较多时,找到需要的异常信息就比较痛苦,

因此,我想到如果在所有异常信息中按关键字搜索到自己想要的信息,最后打印到后台或者输出到日志文件,那阅读异常信息时就比较

一幕了然了,也节省了开发时间。下面有一段代码可以解决这种麻烦,有兴趣的朋友不妨试一试。

view plaincopy to clipboardprint?
public static String printExceptionMsg(Exception exception , String keywords){  
        StringBuffer msg = new StringBuffer();  
        if(keywords != null && exception != null){  
            msg.append("\nException message :");  
            msg.append("\n\t" + exception.toString());  
            msg.append("\nException details :");  
            for(StackTraceElement element : exception.getStackTrace()){  
                if(element != null){  
                    if(element.toString().matches(".*" + keywords + ".*")){  
                        msg.append("\n\t" + element.toString());  

发表评论
验证码:
最新评论