System.exit
should only be used in entrypoint methods201 ta.append(username + " Signing Off");
202 oos.writeObject(new userMessage(username, username + "has left the chat", Instant.now()));
203 Thread.sleep(1000);
204 System.exit(0);205 } catch (IOException | InterruptedException e) {
206 e.printStackTrace();
207 }
System.exit
should only be used in entrypoint methods134 System.out.println("Window Closed");
135
136 Thread.sleep(1000);
137 System.exit(0);138 } catch (InterruptedException e1) {
139 e1.printStackTrace();
140 }
System.exit
should only be used in entrypoint methods 82 ta.append(username + " Signing Off");
83 oos.writeObject(new userMessage(username, username + "has left the chat", Instant.now()));
84 Thread.sleep(1000);
85 System.exit(0); 86 } catch (IOException | InterruptedException e) {
87 e.printStackTrace();
88 }
System.exit
should only be used in entrypoint methods 55 System.out.println("Window Closed");
56
57 Thread.sleep(1000);
58 System.exit(0); 59 } catch (InterruptedException e1) {
60 e1.printStackTrace();
61 }
Description
This method invokes System.exit()
, and is called by other code. This can prevent proper error handling and debugging.
Invoking System.exit()
shuts down the entire Java virtual machine. This should only been done when it is appropriate. Such calls make it hard or impossible for your code to be invoked by other code, since an error that causes System.exit()
to be invoked cannot be handled by the calling code at all.
Bad Practice
if (input == null) System.exit(1);
Recommended
Consider throwing an exception on failure instead.
if (input == null) throw new InvalidInputException();
Exceptions
If the code is intended to be called only by an application entrypoint, this issue can safely be ignored. Ensure that such cases are well documented.
References
- Spotbugs - DM_EXIT