NetBeans / Java-断点无法捕获异常

时间:2019-09-17 13:48:36

标签: java

在NetBeans / Java中-异常断点(用于Throwable)不捕获异常(servlet库代码中的RuntimeException)。如果您进行繁琐的搜索,则可以找到紫色的停止线,但无需查看变量的状态等。VS.NET没有这种疾病-它在需要的地方停止,一切都清楚了。

这是NetBeans问题还是Java?如何摆脱断点问题?

1 个答案:

答案 0 :(得分:0)

在此示例中,您将基本了解Servlet中的异常处理

实际上放置一个断点,因为它在到达终点之前就发生了,所以您无法捕获servlet异常,那么您必须执行类似的操作

当servlet产生错误时,开发人员可以以各种方式处理这些异常

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/SampleExceptionHandler")
public class SampleExceptionHandler extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        exceptionProcessor(request, response);
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        exceptionProcessor(request, response);
    }

    private void exceptionProcessor(HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        // Analyze the servlet exception
        Throwable throwable = (Throwable) request
                .getAttribute("javax.servlet.error.exception");
        Integer statusCode = (Integer) request
                .getAttribute("javax.servlet.error.status_code");
        String servletName = (String) request
                .getAttribute("javax.servlet.error.servlet_name");
        if (servletName == null) {
            servletName = "Unknown";
        }
        String requestUri = (String) request
                .getAttribute("javax.servlet.error.request_uri");
        if (requestUri == null) {
            requestUri = "Unknown";
        }


    }
}