Source: Medium This post will help you better understand how to handle exceptions effectively in Java.
Handling Java exceptions can be challenging. It can be difficult to determine which Java exceptions to handle, which exceptions to rethrow, and which ones to simply ignore. However, there are a number of useful guidelines that can effectively help you handle exceptions and create new applications. Let's get acquainted with them.

- Be careful what you log.
- Don't ignore thrown exceptions.
- Use a global exception handler.
- Don't close resources manually.
- Throw early and handle exceptions later.
- Java exceptions should not be logged and rethrown.
- Examine suppressed exceptions.
- In the throw statement, declare exceptions precisely.
- Handle the most obvious exception first.
- Use modern exception handling semantics.
1. Be careful what you log
Software developers should always be aware of their clients' rights to security and privacy.- A Java program can move data to other systems, and a huge number of developers and administrators can work with it (including fixing bugs).
- If any sensitive data is recorded in the log files, your company will not be compliant with security requirements and you may lose your job.
2. Don't ignore thrown exceptions
Don't catch an exception and then ignore it. Hiding exceptions is not a good way to handle them in Java. At a minimum, write down the name of the exception and the associated message. This way, information about the problem can be found in your entry. Debugging Java applications due to hidden exceptions is incredibly difficult.3. Use a global exception handler
During program execution there will always be some exceptions that were not previously caught in your code. Therefore, always add a global exception handler to handle uncaught exceptions. This will not only allow the exception to be logged and handled, but will also prevent the application from crashing if an exception is thrown at runtime.4. Don't close resources manually
Another important recommendation when handling Java exceptions is to allow the JVM to call the close() method on the resources being closed. Do not try to close them yourself. This can be easily achieved by initializing the resources inside the try-resources module . Semantics:public class TryWithResourcesExample {
public static void main(String[] args) throws Exception {
try (Door door = new Door()) {
door.swing();
} catch (Exception e) { ..... }
} finally { .....}
}
}
As you can see, after the try...catch block completes, the JVM closes the resource for you, eliminating the possibility of complex and hard-to-fix resource leaks.
5. Throw early and handle exceptions later
Throw an exception whenever an exception condition occurs in your code. Don't wait for any lines of code to execute before exiting the method you're in. When catching exceptions, the function must be placed at the end of the method. This reduces the number of catch blocks in your methods, making your code easier to read and maintain.6. Java exceptions should not be logged and rethrown
If an exception occurs, do one of the following:- Continue working with your program while you log the exception.
- Throw the exception again and allow a different way of logging data.
try {
Class.forName("com.min.Example");
} catch (ClassNotFoundException ex) {
log.warning("Class was not found.");
throw ex;
}
This results in code duplication and log files becoming clogged with duplicate entries, making it much more difficult to diagnose the code.
GO TO FULL VERSION