zoukankan      html  css  js  c++  java
  • Java学习

    今天学习了使用Exception对象

    exception对象是Throwable子类的一个实例,只在错误页面中可用。下表列出了Throwable类中一些重要的方法:

    序号方法&描述
    1 public String getMessage() 返回异常的信息。这个信息在Throwable构造函数中被初始化
    2 public ThrowablegetCause() 返回引起异常的原因,类型为Throwable对象
    3 public String toString() 返回类名
    4 public void printStackTrace() 将异常栈轨迹输出至System.err
    5 public StackTraceElement [] getStackTrace() 以栈轨迹元素数组的形式返回异常栈轨迹
    6 public ThrowablefillInStackTrace() 使用当前栈轨迹填充Throwable对象

    JSP提供了可选项来为每个JSP页面指定错误页面。无论何时页面抛出了异常,JSP容器都会自动地调用错误页面。

    接下来的例子为main.jsp指定了一个错误页面。使用<%@page errorPage="XXXXX"%>指令指定一个错误页面。

    <%@ page errorPage="ShowError.jsp" %>
    
    <html>
    <head>
       <title>Error Handling Example</title>
    </head>
    <body>
    <%
       // Throw an exception to invoke the error page
       int x = 1;
       if (x == 1)
       {
          throw new RuntimeException("Error condition!!!");
       }
    %>
    </body>
    </html>

    现在,编写ShowError.jsp文件如下:

    <%@ page isErrorPage="true" %>
    <html>
    <head>
    <title>Show Error Page</title>
    </head>
    <body>
    <h1>Opps...</h1>
    <p>Sorry, an error occurred.</p>
    <p>Here is the exception stack trace: </p>
    <pre>
    <% exception.printStackTrace(response.getWriter()); %>

    注意到,ShowError.jsp文件使用了<%@page isErrorPage="true"%>指令,这个指令告诉JSP编译器需要产生一个异常实例变量。

    现在试着访问main.jsp页面,它将会产生如下结果:

    java.lang.RuntimeException: Error condition!!!
    ......
    
    Opps...
    Sorry, an error occurred.
    
    Here is the exception stack trace:
  • 相关阅读:
    [BZOJ]1040: [ZJOI2008]骑士
    [BZOJ]1177: [Apio2009]Oil
    【luogu3384】【模板】树链剖分
    【NOIP2012TG】solution
    【NOIP2014TG】solution
    【NOIP2016TG】solution
    【NOIP2015TG】solution
    【NOIP2016】【LCA】【树上差分】【史诗级难度】天天爱跑步
    【网络流】【BZOJ1221】【HNOI2001】软件开发
    【网络流】【BZOJ1061】【NOI2008】志愿者招募
  • 原文地址:https://www.cnblogs.com/wrljzb/p/14170809.html
Copyright © 2011-2022 走看看