The 10 best exception handling tips in Java programming

  • 2020-04-01 03:36:56
  • OfStack

In practice, exception handling is more than just knowing the syntax. Writing robust code is more of an art, and in this article, we'll discuss Java exception-handling best practices. These Java best practices follow the standard JDK libraries, and several open source code for handling errors and exceptions. It is also a handy manual for Java programmers to write robust code. Best practices for exception handling in Java programming

Here are my 10 best practices for exception handling in Java programming. Checking for exceptions in Java programming has its critics and critics, and enforcing exceptions is a feature of the language. In this article, we will minimize the use of checked exceptions and learn to use checked VS non-checked exceptions in Java programming.

1) use checked exceptions for recoverable errors and non-checked errors for programming errors.

The choice between checking and non-checking exceptions is always confusing to Java programmers. Checking for exceptions ensures that you provide exception handling code for error conditions, which is a way to force you to write robust code from the language, but also introduces a lot of messy code and makes it unreadable. Of course, if you have alternatives and recovery strategies, it seems reasonable to catch exceptions and do something about them. Select checked exceptions or runtime exceptions in Java programming. Check checked vs unchecked exceptions for more information.

2) close or release resources ina finally block

This is a well-known best practice in Java programming and ACTS as a standard when working with networks and IO classes. Closing the resource ina finally block guarantees a reasonable release of the prior and scarce resource in the case of 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. Still, we need to remember to close the resource in the finally block, which is important to free up limited resources such as FileDescriptors in the case of socket and file programming.

3) include the cause of the exception in the stack trace

Many times, when an exception caused by another exception is thrown, Java libraries and open source wrap one exception into another. Logging and printing root exceptions become important. The Java exception class provides a getCause() method to retrieve the cause of the exception, which provides more information about the root level cause of the exception. This Java practice goes a long way in debugging or troubleshooting. Keep in mind that if you wrap an exception into another exception, a new exception is constructed to pass the source exception.

4) always provide meaningful and complete information about exceptions

The exception information is the most important place, because this is the first place the programmer sees, where you can find the root cause of the problem. Accurate and truthful information is always provided here. For example, compare the two exception messages for an IllegalArgumentException exception:

Message 1: "Incorrect argument for method"
Message 2: "Illegal value for ${argument}: ${value}

The first message says only that the parameter is illegal or incorrect, but the second message includes the parameter name and the illegal value, which is important to find the cause of the error. Always follow this Java best practice when writing exception handling code in Java programming.

5) avoid overusing checker exceptions

Checking exceptions have some advantages in enforcing them, but they also break the code, making it less readable by masking the business logic. As long as you don't overuse checker exceptions, you can minimize them, and the result is cleaner code. You can also use new Java7 features like one catch block for multiple exceptions and automatic resource management to remove duplicates.

6) turn check exceptions into runtime exceptions

This is one of the techniques used in most frameworks like Spring to limit the use of checker exceptions, most of which come from JDBC and are wrapped into DataAccessException, which is a non-checker exception. This is the benefit of Java best practices that restrict specific exceptions to specific modules, such as SQLException to the DAO layer, and throw explicit runtime exceptions to the customer layer.

7) remember that exceptions are expensive for performance

One thing to keep in mind is that exceptions are expensive and make your code run slowly. If you have a way to read from an ResultSet, an SQLException will often be thrown without moving to the next element, which is much slower than normal code that doesn't throw an exception. So minimize unnecessary exception catching and movement where there is no fixed cause. Instead of just throwing and catching exceptions, you might get a cleaner, higher-performance solution if you could use Boolean variables to represent the execution results. Fix the source of the error to avoid unnecessary exception catching.

8) avoid empty catch blocks

There is nothing worse than an empty catch block, because it not only hides errors and exceptions, but can also leave your object in an unusable or dirty state. An empty catch block can only become meaningless if you are sure that the exception will not continue to affect the object state in any way, but it is still best to log the errors during the execution of the program. This is not just a Java best practice for writing exception-handling code in Java programming, but one of the most common.

9) use standard exceptions

Our ninth best practice recommends using standards and built-in Java exceptions. Using standard exceptions instead of creating our own every time is the best option for maintainability and consistency, now and in the future. Reuse standard exception to make your code more readable, because most Java developers to the standard as derived from the JDK RuntimeException anomaly, an IllegalStateException abnormalities, IllegalArgumentException exception or a NullPointerException, (developer) they can instantly know the purpose of each anomaly, rather than to find in the code or in the document for the purpose of the user-defined exception.

10) record any exceptions thrown by a method

Java provides throw and throws keywords to throw exceptions, and in javadoc @throw is used to record any exceptions that a method might throw. This becomes very important if you write apis or public interfaces. Any exception thrown by a method is documented so that you can automatically alert anyone who USES it. These are all the best practices to follow when handling exceptions in Java programming. Let's take a look at the practices you need to follow when writing exception handling code in Java programming.


Related articles: