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. ...

August 8, 2021

Exception handling - Java Exception Handling (Theory)

Parent Topic Exception handling journey References clean code effective java oracle java docs This time, let’s talk about what an exception is and how to handle exceptions. What is an exception? Let’s see how Oracle defines it: An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions. It means an event that disrupts the normal flow during program execution. ...

August 8, 2021

Exception handling journey - Redirection

Parent Topic Exception handling journey References https://opentutorials.org/course/2598/14199 https://en.wikipedia.org/wiki/Standard_streams https://en.wikipedia.org/wiki/Redirection_(computing) Definition Redirection is a form of interprocess communication (IPC). It is a function that most command line interpreters have. This is the method that came up when I wondered how data entered in the console is delivered to the app. Cat Actually, the definition and explanation are so well explained by Life Coding, so I’ll just link to it. https://opentutorials.org/course/2598/14199 Java What I want to test is how Java handles input and output. I want to know when stdin, stdout, and stderr are used. ...

July 3, 2021

From Keyboard Input to Console Output

Parent Topic Exception handling journey Situation - Simple Input/Output Program The app receives input from the keyboard. The app prints the input value to the console. This app is run from the console. Additionally, to analyze the process in detail, the program also prints the pid. Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class App { public static void main(String[] args) { printPid(); Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { System.out.println(scanner.next()); } } private static void printPid() { long pid = ProcessHandle.current().pid(); System.out.println("current pid: " + pid); } } Execution After compiling the code above in the console, run it in iterm. ...

July 3, 2021

Exception handling journey

First Step An exception can be seen as an abnormal situation. Excerpt from Oracle docs An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions. Therefore, developers should provide exception messages so that they can understand the situation when an exception occurs. Also, a client who calls the API should be given a different message from the developer. ...

July 3, 2021