Exception异常堆栈信息转String,存数据库
在做需求开发的时候,碰到⼀个需求:某个操作可能会失败(因为数据或其他原因导致了异常),后台只是捕获了异常,然后笼统的提⽰该操作失败。但是这样不能从页⾯直接看到异常原因,由于这个功能是我们公司⾃⼰的⼈员使⽤的,后台领导要求把异常的详细信息都展⽰在页⾯上或者存储到数据库的⽇志表中。这样可以⽅便的去解决这个异常。
Exception的⽅法中,⽤的最多的是getMessage()和printStackTrace(),前者获取的信息太少,⽆法定位到异常出现的类、⽅法和⾏数;后者可以打印异常堆栈的详细信息,但是它没有返回值,我们也就⽆法获取到信息进⾏展⽰和存储。通过查询资料,发现
ption包下⾯有⼀个异常相关的⼯具类:ExceptionUtils,它⾥⾯有⼀个⽅法,可以获取到异常对象Exception的完整信息,并且是String类型的:
//拿到⽇志栈的完整信息
String fullStackTrace = FullStackTrace(e);
大宇神秘惊奇系列它得到的信息是详细下堆栈信息,举例如下:
java.lang.StringIndexOutOfBoundsException: String index out of range: -13
at java.lang.String.substring(String.java:1931)
at com.chinacreator.approve.rvice.LocalOrganizationService_new.handlerXzcfData_new(LocalOrganizationService_new.java:1224)
拥抱的温度只有你清楚是什么歌at com.chinacreator.approve.rvice.LocalOrganizationService_new.updateApproveXzcf_new(LocalOrganizationService_new.java:1183)
at com.chinacreator.approve.rvice.OrgOperationForUpdate(LocalOrganizationService_new.java:973)
at com.chinacreator.approve.rvice.LocalOrganizationService_new$$FastClassBySpringCGLIB$$61ca30de.invoke(<generated>)
at lib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:700)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at ansaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
at ansaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
at ansaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvidInterceptor.intercept(CglibAopProxy.java:633)
at com.chinacreator.approve.rvice.LocalOrganizationService_new$$EnhancerBySpringCGLIB$$OrgOperationForUpdate(<generated>) at com.chinacreator.Org(LocalOrganizationController.java:246)
flect.GeneratedMethodAccessor799.invoke(Unknown Source)
flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at flect.Method.invoke(Method.java:498)
at MethodInjectorImpl.invoke(MethodInjectorImpl.java:139)
at ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:295)
at ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:249)
at ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:236)
at steasy.ateModelAndView(ResteasyHandlerAdapter.java:96)
我有一个梦想演讲稿马丁路德金at steasy.springmvc.ResteasyHandlerAdapter.handle(ResteasyHandlerAdapter.java:82)
at steasy.springmvc.ResteasyHandlerAdapter.handle(ResteasyHandlerAdapter.java:26)
at steasy.springmvc.ResteasyWebHandlerTemplate.handle(ResteasyWebHandlerTemplate.java:39)
at steasy.springmvc.ResteasyHandlerAdapter.handle(ResteasyHandlerAdapter.java:47)
at org.springframework.web.rvlet.DispatcherServlet.doDispatch(DispatcherServlet.java:919)
at org.springframework.web.rvlet.DispatcherServlet.doService(DispatcherServlet.java:851)
at org.springframework.web.rvlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
at org.springframework.web.rvlet.FrameworkServlet.doPost(FrameworkServlet.java:855)
at javax.rvlet.http.HttpServlet.rvice(HttpServlet.java:755)
草莓种子怎么种at org.springframework.web.rvlet.FrameworkServlet.rvice(FrameworkServlet.java:829)
at javax.rvlet.http.HttpServlet.rvice(HttpServlet.java:848)
lip.jetty.rvlet.ServletHolder.handle(ServletHolder.java:684)
lip.jetty.rvlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1496)
at com.p.TokenCheckSuccess(DefaultSpi.java:67)
at com.p.sys.oauth2.sso.client.filter.OAuth2SSOFilter.handleInnerCall(OAuth2SSOFilter.java:537)
at com.p.sys.oauth2.sso.client.filter.OAuth2SSOFilter.doAuthcFilter(OAuth2SSOFilter.java:463)
at com.p.sys.oauth2.sso.client.filter.OAuth2SSOFilter.doFilter(OAuth2SSOFilter.java:99)
lip.jetty.rvlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1484)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
lip.jetty.rvlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1484)
at filter.AccessFilter.doFilter(AccessFilter.java:35)
at filter.AccessFilter.doFilter(AccessFilter.java:35)
lip.jetty.rvlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1484)
at com.chinacreator.c2.web.filter.GZIPFilter.doFilter(GZIPFilter.java:74)
聚集反义词at com.chinacreator.c2.web.filter.Filter.doFilter(Filter.java:69)
lip.jetty.rvlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1476)
lip.jetty.rvlet.ServletHandler.doHandle(ServletHandler.java:499)
lip.jetty.rver.handler.ScopedHandler.handle(ScopedHandler.java:137)
lip.jetty.curity.SecurityHandler.handle(SecurityHandler.java:557)
lip.jetty.rver.ssion.SessionHandler.doHandle(SessionHandler.java:231)
lip.jetty.rver.handler.ContextHandler.doHandle(ContextHandler.java:1086)
lip.jetty.rvlet.ServletHandler.doScope(ServletHandler.java:428)
lip.jetty.rver.ssion.SessionHandler.doScope(SessionHandler.java:193)
lip.jetty.rver.handler.ContextHandler.doScope(ContextHandler.java:1020)
lip.jetty.rver.handler.ScopedHandler.handle(ScopedHandler.java:135)
lip.jetty.rver.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
lip.jetty.rver.Server.handle(Server.java:370)
lip.jetty.rver.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494)
lip.jetty.t(AbstractHttpConnection.java:982)
lip.jetty.rver.t(AbstractHttpConnection.java:1043)
扬州旅游景点lip.jetty.http.HttpParr.parNext(HttpParr.java:865)
lip.jetty.http.HttpParr.parAvailable(HttpParr.java:240)
lip.jetty.rver.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
lip.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:667)
lip.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
lip.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
lip.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
视频资源网站at java.lang.Thread.run(Thread.java:748)
可以看到它的内容是⽐较多的,如果全部显⽰在页⾯不太合适,所以可以只截取前⾯那些跟我们⾃⼰的代码相关的即可,因此最终我的处理⽅式如下:
} catch (Exception e) {
e.printStackTrace();
//拿到⽇志栈的完整信息
String fullStackTrace = FullStackTrace(e);
if(fullStackTrace.length()>1000){
fullStackTrace = fullStackTrace.substring(0,1000);
}
<("修改⽬录引⽤表失败,异常信息:" + e.toString());
我是一颗小小的石头
throw new RuntimeException("修改⽬录引⽤表失败,异常信息:" + fullStackTrace);
}