📜  JSP-异常处理

📅  最后修改于: 2020-11-13 05:30:43             🧑  作者: Mango


在这一章当中。我们将讨论如何在JSP中处理异常。编写JSP代码时,可能会出现编码错误,该错误可能发生在代码的任何部分。您的JSP代码中可能会发生以下类型的错误-

检查异常

受检查的异常是通常为用户错误或程序员无法预见的问题的异常。例如,如果要打开文件,但找不到该文件,则会发生异常。这些异常不能在编译时简单地忽略。

运行时异常

运行时异常是程序员可能避免的异常。与检查的异常相反,在编译时会忽略运行时异常。

失误

这些根本不是例外,而是超出用户或程序员控制范围而产生的问题。错误通常会在代码中被忽略,因为您几乎无法对错误做任何事情。例如,如果发生堆栈溢出,则会出现错误。在编译时也将忽略它们。

我们将进一步讨论处理JSP代码中发生的运行时异常/错误的方法。

使用异常对象

异常对象是Throwable子类的实例(例如,java.lang。NullPointerException),并且仅在错误页面中可用。下表列出了Throwable类中可用的重要方法。

S.No. Methods & Description
1

public String getMessage()

Returns a detailed message about the exception that has occurred. This message is initialized in the Throwable constructor.

2

public Throwable getCause()

Returns the cause of the exception as represented by a Throwable object.

3

public String toString()

Returns the name of the class concatenated with the result of getMessage().

4

public void printStackTrace()

Prints the result of toString() along with the stack trace to System.err, the error output stream.

5

public StackTraceElement [] getStackTrace()

Returns an array containing each element on the stack trace. The element at index 0 represents the top of the call stack, and the last element in the array represents the method at the bottom of the call stack.

6

public Throwable fillInStackTrace()

Fills the stack trace of this Throwable object with the current stack trace, adding to any previous information in the stack trace.

JSP为您提供了一个选项,可以为每个JSP指定错误页面。每当页面抛出异常时,JSP容器都会自动调用错误页面。

以下是为main.jsp指定错误页面的示例。要设置错误页面,请使用<%@页面errorPage =“ xxx”%>指令。

Error Handling Example
   
   
   
      
   

现在,我们将编写一个错误处理JSP ShowError.jsp,如下所示。请注意,错误处理页面包括指令<%@ page isErrorPage =“ true”%> 。该指令使JSP编译器生成异常实例变量。

Show Error Page
   
   
   
      

Opps...

Sorry, an error occurred.

Here is the exception stack trace:


   

访问main.jsp ,您将收到类似于以下内容的输出-

java.lang.RuntimeException: Error condition!!!
......

Opps...
Sorry, an error occurred.

Here is the exception stack trace:

对错误页面使用JSTL标签

您可以使用JSTL标记来编写错误页面ShowError.jsp 。此页面的逻辑与上述示例几乎相同,但结构更好,信息更多-

Show Error Page
   
   
   
      

Opps...

Error:

java.lang.RuntimeException: Error condition!!!

URI:

/main.jsp

Status code:

500

Stack trace:

org.apache.jsp.main_jsp._jspService(main_jsp.java:65)

org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:68)

javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)

javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

访问main.jsp,将生成以下内容-

Opps...

错误:

java.lang.RuntimeException:错误情况!!!

URI:

/main.jsp

状态码:

500

堆栈跟踪:

org.apache.jsp.main_jsp._jspService(main_jsp.java:65)

org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:68)

javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)

javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

使用Try … Catch块

如果要处理同一页面中的错误,并且想要采取某些措施而不是触发错误页面,则可以使用try …. catch块。

以下是一个简单的示例,显示了如何使用try … catch块。让我们将以下代码放入main.jsp中:

Try...Catch Example
   
   
   
      
   

访问main.jsp,它应该生成类似于以下内容的输出-

An exception occurred: / by zero