Java - Exception Handling (Code)
Parent Topic Exception handling journey Situation Here is some smelly code I used incorrectly. Situation A function receives an order number and returns the corresponding order sheet. If the requested order number does not exist, an error should be raised and the client should be notified that the order does not exist. Code Controller 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package com.company.exceptiontest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Controller { private final Service service; private final Logger logger = LoggerFactory.getLogger(this.getClass()); public Controller(Service service) { this.service = service; } public String orderInfo(Long id) { try { OrderSheet orderSheet = service.searchOrderSheet(id); return String.valueOf(orderSheet); } catch (CustomException e) { logger.info("controller - orderInfo method: Error occurred while requesting order sheet info!"); e.printStackTrace(); return e.getMessage(); } } } In the controller, the order sheet information is returned as a string. If an error occurs, it is caught, logged, and the error message is returned to the user. ...