In practice, exception handling is not simply about knowing the syntax. Writing robust code is more like an art, and in this article, Java exception handling best practices are discussed. These Java best practices follow the standard JDK library, and several open source code that handles errors and exceptions. This is also a handy manual for Java programmers to write robust code. Best practices for exception handling in Java programming
Here are 10 best practices for exception handling in 10 Java programming that I've collected. In Java programming, it is the function of a language to handle exceptions in a derogatory sense. In this article, we will minimize the use of check-type exceptions and learn to use check vs unchecked exceptions in Java programming.
1 Use check-type exceptions for recoverable errors, and non-check errors for programming errors.
Choosing a check or an unchecked exception is always confusing to Java programmers. Check-type exceptions ensure that you provide exception handling code for error conditions, a way from language to forcing you to write robust code, but at the same time introduce a lot of messy code and cause it to be unreadable. Of course, if you have alternatives and a recovery strategy, it seems reasonable to catch the exception and do something. Select Check-type exceptions or Run-time Exceptions in Java programming, more information reference checked vs unchecked exceptions.
2) to shut down or release resources in the finally program block
This is a well-known best practice in Java programming, which is equivalent to a standard when dealing with networks and IO classes. Close the resource in the finally block, guaranteeing the reasonable release of the prior and scarce resources in normal and abnormal execution, which is guaranteed by the Y finally block. Starting with Java7, the language has a more interesting feature: resource management automation or arm blocks to achieve this. Nevertheless, we still have to remember to shut down resources in the finally block, which is important for releasing limited resources such as filedescriptors, which are applied in sockets and file programming.
3 contains the cause of the exception in the stack trace
Many times, when an exception caused by another exception is thrown, the Java library and the open source wrap an exception into another exception. Logging and printing root exceptions become very important. The Java exception class provides a Getcause () method to retrieve the cause of the exception, which can provide more information about the cause of the root level of the exception. This Java practice is helpful in debugging or troubleshooting. Always remember that if you wrap an exception into another exception, construct a new exception to pass the source exception.
4 always provide meaningful and complete information about the exception
Exception information is the most important place, because this is the first place that programmers see first, where you can find the root cause of the problem. It always provides accurate and truthful information. For example, compare the two exception information for the illegalargumentexception exception:
Message 1: "Incorrect argument for method"
Message 2: "Illegal value for ${argument}: ${value}
The first message only indicates that the argument is illegal or incorrect, but the second message includes the parameter name and the illegal value, which is important for finding the cause of the error. Always follow this Java best practice when writing exception handling code in Java programming.
5 Avoid excessive use of check-type exceptions
Check-type exceptions have some advantage in enforcement, but at the same time it destroys code and makes code less readable by masking business logic. As long as you do not overuse the check-type exception, you can minimize this kind of situation, the result is that you will get cleaner code. You can also use the new features of Java7, like one catch block for multiple exceptions and automatic resource management to remove duplicates.
6 Change the check-type exception to a Run-time exception
This is one of the techniques used in most frameworks, like spring, to restrict the use of check-type exceptions, most of which come from JDBC check-type exceptions that are packaged into DataAccessException, The (DataAccessException) exception is a non checked type exception. This is the benefit of Java best practices, where specific exceptions are restricted to specific modules, like SQLException to the DAO layer, which throws explicit Run-time exceptions to the customer layer.
7 Remember that for performance, it's extremely expensive
One thing to keep in mind is that it's unusually expensive, while keeping your code running slowly. If you have a way to read from a resultset (result set), you often throw a SqlException exception without moving to the next element, which is much slower than normal code that does not throw an exception. Thus minimizing unnecessary anomaly capture and movement, there is no fixed cause. Don't just throw and catch exceptions, if you can use Boolean variables to represent execution results, you may get a cleaner, more high-performance solution. Fix the source of the error and avoid the exception capture that is not necessary.
8 prevent catch blocks from being empty
Nothing is worse than an empty catch block, as it not only hides errors and exceptions, but it can also cause your object to be in a state of being unusable or dirty. An empty catch block can only become meaningless, and if you are very certain that an exception will not continue to affect the state of the object in any way, it is still the best method to log errors during program execution. For writing exception handling code in Java programming, this is not just a Java best practice, but a most common practice.
9) using standard exceptions
Our nineth best practice recommends using standard and built-in Java exceptions. Using standard exceptions instead of creating our own exceptions every time is the best choice for maintainability and consistency, both now and in the future. Reusing standard exceptions makes the code more readable because most Java developers have a runtimeexception exception, illegalstateexception exception, that is derived from the JDK, illegalargumentexception Exceptions or NullPointerException exceptions, (developers) they can know the purpose of each exception at a glance, rather than looking up in the code or looking for a user-defined exception in the document.
10 record any exceptions thrown by the method
Java provides throw and throws keywords to throw exceptions, and use @throw in Javadoc to record exceptions that any method might throw. This becomes very important if you write APIs or public interfaces. Any exception that is thrown by any method has a corresponding document record, so you can subconsciously remind anyone who uses it. These are all the best practices to follow when handling exceptions in Java programming. Let's find out what is the practice that you need to follow to write exception handling code in Java programming.