<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # JSP異常處理 當編寫JSP程序的時候,程序員可能會遺漏一些BUG,這些BUG可能會出現在程序的任何地方。JSP代碼中通常有以下幾類異常: * 檢查型異常:檢查型異常就是一個典型的用戶錯誤或者一個程序員無法預見的錯誤。舉例來說,如果一個文件將要被打開,但是無法找到這個文件,則一個異常被拋出。這些異常不能在編譯期被簡單地忽略。 * 運行時異常:一個運行時異常可能已經被程序員避免,這種異常在編譯期將會被忽略。 * 錯誤:錯誤不是異常,但問題是它超出了用戶或者程序員的控制范圍。錯誤通常會在代碼中被忽略,您幾乎不能拿它怎么樣。舉例來說,棧溢出錯誤。這些錯誤都會在編譯期被忽略。 本節將會給出幾個簡單而優雅的方式來處理運行時異常和錯誤。 * * * ## 使用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="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文件使用了指令,這個指令告訴JSP編譯器需要產生一個異常實例變量。 現在試著訪問main.jsp頁面,它將會產生如下結果: ~~~ java.lang.RuntimeException: Error condition!!! ...... Opps... Sorry, an error occurred. Here is the exception stack trace: ~~~ * * * ## 在錯誤頁面中使用JSTL標簽 可以利用JSTL標簽來編寫錯誤頁面ShowError.jsp。這個例子中的代碼與上例代碼的邏輯幾乎一樣,但是本例的代碼有更好的結構,并且能夠提供更多信息: ~~~ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@page isErrorPage="true" %> <html> <head> <title>Show Error Page</title> </head> <body> <h1>Opps...</h1> <table width="100%" border="1"> <tr valign="top"> <td width="40%"><b>Error:</b></td> <td>${pageContext.exception}</td> </tr> <tr valign="top"> <td><b>URI:</b></td> <td>${pageContext.errorData.requestURI}</td> </tr> <tr valign="top"> <td><b>Status code:</b></td> <td>${pageContext.errorData.statusCode}</td> </tr> <tr valign="top"> <td><b>Stack trace:</b></td> <td> <c:forEach var="trace" items="${pageContext.exception.stackTrace}"> <p>${trace}</p> </c:forEach> </td> </tr> </table> </body> </html> ~~~ 運行結果如下: ![jsp-exeception-1](https://www.runoob.com/wp-content/uploads/2014/01/jsp-exeception-1.jpg) * * * ## 使用 try…catch塊 如果您想要將異常處理放在一個頁面中,并且對不同的異常進行不同的處理,那么您就需要使用try…catch塊了。 接下來的這個例子顯示了如何使用try…catch塊,將這些代碼放在main.jsp中: ~~~ <html> <head> <title>Try...Catch Example</title> </head> <body> <% try{ int i = 1; i = i / 0; out.println("The answer is " + i); } catch (Exception e){ out.println("An exception occurred: " + e.getMessage()); } %> </body> </html> ~~~ 試著訪問main.jsp,它將會產生如下結果: ~~~ An exception occurred: / by zero ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看