Java software development is often what is abnormal? How to deal with?

  Exception handling is an important part of the Java software development, it is related to a non-functional requirements of each application, to handle any error conditions, such as resource inaccessible, illegal input, air input, etc., Java provides several exception handling features to try, in the form of catch and finally keywords built into the language itself, Java programming language also allows you to create a new exception, throw and throw and throws them by using keywords, in fact, in Java programming, Java exception handling is not just know the syntax so simple, it must follow the standard JDK libraries, and a few handling errors and exceptions of open source, where we will discuss some best practices for Java exception handling.


  As a recoverable error using checked exceptions for programming errors using unchecked error


  Select the type of check or unchecked exceptions, for Java programmers who are always confusing. Checked exceptions guarantee that you provide exception handling code error condition, which is a language from a way to force you to write code robust, but will also introduce a lot of messy code and cause it unreadable. Of course, if you have alternatives and recovery strategy, then catch the exception and do something also seems reasonable to select abnormal abnormal or run-time type checking in Java programming.


  Closing or releasing resources in the finally block


  This is in Java programming, is a well-known best practice, when dealing with network and IO class, equivalent to a standard. Closed in the finally block resources, under normal and abnormal situation performed before release to ensure the rational and scarce resources, which is guaranteed by the finally block. Java7 from the beginning, the language has a more interesting features: resource management automation or ARM blocks can achieve this functionality. Nevertheless, we still have to remember to close the resource in the finally block, which is released as FileDescriptors for this type of application in the case of socket programming and documentation of limited resources is important.


  Use standard exceptions


  We recommend the use of Article IX of standards and best practices built-in Java exceptions. Use standard exception rather than each create our own exception, for the maintenance and consistency, either now or in the future, they are the best choice. Reuse of standard exceptions make the code more readable, because most Java developers to like a standard derived from the JDK's a RuntimeException, IllegalStateException anomaly, IllegalArgumentException or a NullPointerException abnormal, (developers) that they can know at a glance Each exception the purpose, instead of looking at the code or find the exception of user-defined purpose in the document.


  Record any exception thrown by method


  Java programming language provides throws and throw keywords to throw an exception, recording method may throw any exception in using @throw javadoc. If you write a public interface or API, which becomes very important. Any method throws exceptions have the appropriate documentation, so you can use any subconscious reminder (which) people.


  These are all the best practices in Java programming to be followed in the handling of an exception and let us know what is the practice to follow when you write exception handling code in Java programming.

Reproduced in: https: //www.jianshu.com/p/1447e8169008

Guess you like

Origin blog.csdn.net/weixin_34342905/article/details/91275756