简体   繁体   中英

How not to print stacktrace on the browser

I am working on a web service. Sometimes during my testing exceptions are thrown and displayed on the browser page. I am not sure why even the code that "could" throw an exception is within the try-catch block, the exception is still printed in the browser.

I certainly can create exception handler with Spring or exception mapper (with JBoss) but still there are exceptions that could be thrown that are not mapped, and I don't want this to be printed on the browser.

How can I fix this?

You might be catching checked exceptions and those shown on the browser might be unchecked exceptions. One way is to catch RuntimeException which is the parent for all unchecked exceptions and display a meaningful error in the browser. If you are the owner of the APIs throwing such exceptions, it is worth going through this small notes on exceptions .

in your web.xml you can define error pages like this

<error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/jsp/error.jsp</location>
</error-page>

<error-page>
    <error-code>403</error-code>
    <location>/WEB_INF/jsp/accessDenied.jsp</location>
</error-page>

and so on... you can have for any other error code especially error 500 witch i think is more suitable for your case

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM