手动处理异常的使用

     在程序运行过程中,不免遇到一些错误,这时候就要对错误进行一些处理,使得错误能够更友好的展示给用户。所以在这介绍一下手动错误处理的使用:
1、建立错误处理程序的类,继承自 RuntimeException  ,如下
public class ApplicationException extends RuntimeException {
    public ApplicationException() {
    }
    public ApplicationException(String message) {
        super(message);
    }
    public ApplicationException(Throwable cause) {
        super(cause);
    }
    public ApplicationException(String message, Throwable cause) {
        super(message, cause);
    }
    public ApplicationException(String message, Throwable cause,
            boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}

2、错误处理配置
    <!-- 错误跳转页面的配置 -->

     <error-page>
         <exception-type>com.bjpowernode.drp.util.ApplicationException</exception-type>
         <location>/error.jsp</location>
     </error-page>  

3、错误处理程序error.jsp(简单拦截)
<div align="center"><p align="left"><span class="STYLE1">错误信息:</div>
    <div><%=exception.getMessage()%></div>

4、除此之外,还有对一些错误码的如理,如下:
        1)配置文件中对错误码的配置
<!-- 错误跳转页面的配置 -->
     <error-page>
         <error-code>404</error-code>
         <location>/http_error.jsp</location>
     </error-page>
     <error-page>
         <error-code>500</error-code>
         <location>/http_error.jsp</location>
     </error-page>  

         2)错误处理页面 http_error.jsp的处理   
        <body>
            <%
                Integer errorCode=(Integer)request.getAttribute("javax.servlet.error.status_code");
                 //当分别遇到错误码为404和500时,分别跳转到相应的错误处理页面进行处理

                if (errorCode==404){
                    response.sendRedirect(request.getContextPath()+ "/404.jsp");
                }else if (errorCode==500){
                    response.sendRedirect(request.getContextPath()+ "/500.jsp");
                }
            %>

        </body>

  3)错误处理页面处理
    如果是404: < body >  未找到请求的页面 </ body >
    如果是500: < body > 系统出现错误,请与系统管理员联系! </body>       
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值