简体   繁体   中英

Declaring to throw an exception without ever throwing it

I have a couple of EJBs that are called from a jax-rs web service. However, every time I call one of the EJB methods I am intercepting the invocation using an interceptor. This interceptor checks the parameters for an API key and if it is not present or doesnt exist in my database it will throw an exception that I wish to catch in my jax-rs code. However, since my jax-rs class has no knowledge of the possibility of this exception being thrown even though the exception could be a checked exception, what would be the best practice here?

  1. Declaring my EJB to throw exceptions (without ever throwing them itself)
  2. Making my exception an unchecked exception and checking for it anyways?
  3. Something else?

I figured out that throwing an unchecked exception would be the best. I then use an exception mapper to catch the exception and handle it.

https://docs.oracle.com/javaee/6/api/javax/ws/rs/ext/ExceptionMapper.html

The only thing that's left is to suppress the exception output to the log files.

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