`
y806839048
  • 浏览: 1132283 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

异常处理

 
阅读更多

 异常处理:http://cgs1999.iteye.com/blog/1547197(包含三种处理方式)

  1,抛出

    1,不包装直接抛出Exception会有系列堆栈信息,同时eclipse控制台也是如此

    2,包装一层---用一个子类继承Exception,不会有堆栈信息只会有我们定义的文字信息,如截图红色框部分

  2,定义全局的异常捕获器捕获处理

    1,拦截器

    2,配置文件

    3,注解

 

 

示例(配置文件):

@Controller 层:

 

 

 @RequestMapping("/account/tbCusPsdReset/update")

    public ModelAndView updatePsd(TbCusFirm tbCusFirm,Model model) throws Exception {

    Subject currentUser = SecurityUtils.getSubject();

Session session1 = currentUser.getSession();//currentUser

OpmUser user1=(OpmUser) session1.getAttribute("currentUser");

    TbCusFirm use = new TbCusFirm();

if(!"".equals(tbCusFirm.getCustomerId())&&null!=tbCusFirm.getCustomerId()&&!"0".equals(tbCusFirm.getCustomerId())){

use.setCustomerId(tbCusFirm.getCustomerId());

use= tbConFirmFeeService.getCustomerInfo(use);

}

if(use==null){

 

throw new Exception("找不到你要重置的会员!");

}

if(!use.getMarketKey().equals(user1.getOrganid())&&!"100000".equals(user1.getOrganid())){

throw new ServiceException("您无权修改密码!");

//throw new Exception("您无权修改密码!");不包装直接抛出Exception会有系列堆栈信息,同时eclipse控制台也是如此

}

}

 

//由于bao

 public Throwable fillInStackTrace() {

return this;

}由于包装的异常类中有这段代码所以不会把堆栈打出

 

配置文件

 

<!-- 总错误处理 http://fancyboy2050.iteye.com/blog/965663 -->

<bean id="exceptionResolver"

class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">

<!--<property name="exceptionAttribute" value="ex"></property> -->  //jsp页面获取异常信息的变量默认是exception

<property name="exceptionMappings">

            <props>

                <prop key="java.lang.Exception">404Exc</prop>  //我们视图解析器下的jsp文件

                <prop key="java.lang.Throwable">404Exc</prop>

            </props>

        </property>

<property name="statusCodes">

            <props>

                <prop key="500">500</prop>

                <prop key="404">404</prop>

            </props>

        </property>

<property name="defaultErrorView">

<value>404</value>

</property>

<property name="defaultStatusCode">

<value>500</value>

</property>

<property name="warnLogCategory">

<value>org.springframework.web.servlet.handler.SimpleMappingExceptionResolver

</value>

</property>

</bean>

 

 

 

404Exc.jsp

<body>

你好

<%

        Exception exception = (Exception) request.getAttribute("exception");

        if (exception != null) {

            out.print(exception+"\n");

            for (StackTraceElement stackTraceElement: exception.getStackTrace()){

                out.print(stackTraceElement.toString()+"\n");

            }

        }

    %>

    </div>

    </body>

 

 

 

 

 

package com.esteel.dwz.framework.sys.exception;

 

/**

 * 系统异常基础实现-checked Exception.<br/>

 * 

 * 希望调用者对抛出的异常进行处理时,可以直接使用此异常或者使用此异常的子类.<br/>

 * 

 * 系统中发生的异常, 都需要重新包装为 ServiceException&ServiceRuntimeException 或者其子类

 * 

 * 实现这个异常的目的:<br/>

 * 1.统一异常的父类,便于系统中对异常做统一处理.<br/>

 * 2.复写fillInStackTrace 方法, 降低创建异常的开销.<br/>

 * <br/>

 * Error日志输出时机:<br/>

 * 重新包装原始异常时, 进行日志输出<br/>

 * <br/>

 * 例如:<br/>

 * <pre>

 * try{

 * ...

 * }catch(XxxException e){

 *     logger.error(e);

 *     //重新包装, 进行日志输出

 *     throw new ServiceRuntimeException(e);

 * }catch(ServiceException e2){

 *     //接收到ServiceException,不做重新包装, 不进行日志输出

 *     throw e2;

 * 

 * }catch(Exception e1){

 *     //重新包装, 进行日志输出

 *   logger.error(e1);

 *   throw new ServiceRuntimeException(e1);

 * }

 * </pre>

 * 

 * 

 * @author zhaozhl

 *

 */

public class ServiceException extends Exception {

 

private static final long serialVersionUID = -6879298763723247455L;

 

public ServiceException() {

super();

}

 

public ServiceException(String message) {

super(message);

}

 

public ServiceException(Throwable cause) {

super(cause);

}

 

public ServiceException(String message, Throwable cause) {

super(message, cause);

}

 

public Throwable fillInStackTrace() {

return this;

}

 

}

 

 

 

 

 

 

同理事务的回滚:

1,@Service  抛出默认的运行时异常(可以配置为Exception)

2,

  1,用注解

  2,用配置文件

3,数据库引擎支持事务(mysql支持事务的引擎innerDb)

  • 大小: 12.9 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics