Java learning-Exception Handling and java Exception Handling

Source: Internet
Author: User

Java learning-Exception Handling and java Exception Handling

An exception cannot be ignored when using Java for software development and test Script Development. Whether to handle exceptions is directly related to the stability and robustness of the developed software.
Exception

Exceptions refer to unexpected situations, such as file failure, network connection failure, and invalid parameters. An exception is an event that interferes with the normal command flow during the running of the program.

Java describes various exceptions through the many subclasses of the Throwable class in the API. Therefore, Java exceptions are all objects and are examples of Throwable sub-classes, which describe the error conditions that appear in a section of encoding. When a condition is generated, an error occurs.

Category

In Java, all exceptions have a common ancestor Throwable ). Throwable specifies the commonalities of any issues that can be transmitted through Java applications through the Exception Propagation mechanism in the code.

  • Throwable:

There are two important sub-classes: Exception (Exception) and Error (Error). Both are important sub-classes for Java Exception Handling, each of which contains a large number of sub-classes.

  • Error)

Indicates a serious problem in running the application. Most errors are irrelevant to the operations performed by the code writer, but they indicate problems with the JVM (Java Virtual Machine) during code execution. For example, when the JVM does not have the memory resources required to continue the operation, an OutOfMemoryError occurs. When these exceptions occur, the Java Virtual Machine (JVM) generally chooses to terminate the thread.

These errors are not traceable because they are beyond the control and processing capabilities of the application, and most of them are not allowed when the program is running. For a well-designed application, even if an error occurs, it is essentially not necessary to try to handle the exception caused by it. In Java, the Error is described by the subclass of Error.

  • Exception (an Exception that the program can handle)

Exceptions are classified into two types: runtime exceptions and non-runtime exceptions (compilation exceptions ). The program should try to handle these exceptions.

1. runtime exception

All are RuntimeException classes and their subclass exceptions, such as NullPointerException (NULL pointer exception) and IndexOutOfBoundsException (subscript out-of-bounds exception). These exceptions are non-check exceptions and can be captured and processed in programs, or not. These exceptions are generally caused by program logic errors. The program should avoid such exceptions logically.

The feature of a row exception is that the Java compiler does not check it. That is to say, when such exceptions may occur in the program, even if the try-catch statement is not used to catch it, The throws clause is not declared to throw it, it will also be compiled.

2. Non-runtime exception (compilation exception)

Is an Exception other than RuntimeException, and belongs to the Exception class and its subclass. From the perspective of program syntax, it is an exception that must be processed. If it is not processed, the program cannot be compiled. Such as IOException, SQLException, and user-defined Exception. Generally, no custom check Exception occurs.

Java Exception Handling principles
  • Handle exceptions as much as possible

To handle exceptions as much as possible, if the conditions are not allowed and cannot be processed in your own code, consider declaring exceptions. If you manually avoid handling exceptions in the Code and make declarations only, this is a practice of errors and dependencies.

  • Solve specific problems

Some of the advantages of exceptions are that they provide different processing operations for different types of problems. The key to effective exception handling is to identify specific fault scenarios and develop specific actions to solve such scenarios. To make full use of exception handling capabilities, you need to build a specific processor block for a specific type of problems.

  • Record exceptions that may affect the application program running

Take at least some permanent methods to record exceptions that may affect application operations. Ideally, it is certainly the first time to solve the basic problem that causes exceptions. However, no matter which processing operation is used, the potential key issues should generally be recorded. Although this operation is simple, it helps you track the cause of complex problems in your application with little time.

  • Convert exceptions to business context as appropriate

To notify an application of a specific issue, it is necessary to convert the application into different forms. If an exception is indicated by a specific service status, the code is easier to maintain. In a sense, whenever an exception is transmitted to a different context (that is, another technical layer), the exception should be converted into a form that is meaningful to the new context.

Java Exception Handling

Manage by five keywords: try, catch, throw, throws, and finally. The basic process is to use the try statement block to wrap the statement to be monitored. If an exception occurs in the try statement block, the exception will be thrown, your code can capture and handle this exception in the catch statement block, and some system-generated exceptions are automatically thrown during Java runtime.

You can also declare the method to throw an exception through the throws keyword, and then throw an exception object through throw in the method. The finally statement block is executed before the return method is executed. The general structure is as follows:

Try {program code} catch (exception type 1 Abnormal variable name 1) {program code} catch (exception type 2 abnormal variable name 2) {program code} finally {program code}

There can be multiple catch statements to match multiple exceptions. After matching one of the above, only the matching exceptions are executed when the catch statement block is executed.

The catch type is defined in the Java language or defined by the programmer. It indicates the type of the Code that throws an exception, and the variable name of the exception indicates the reference of the object that throws the exception, if catch captures and matches the exception, you can directly use the exception variable name. At this time, the exception variable name points to the matched exception and can be referenced directly in the catch code block. This is very special and important!

Summary

The purpose of Java exception handling is to improve the robustness of the program. It is a special feature of the Java language and also a difficult point. Mastering exception handling can make the written code more robust and easy to maintain. More importantly, the idea of java exception handling is worth learning. We must have a long-term vision and a broad view, which will put future errors in the cradle. Of course, ice freezing is not a cold day, and we still need to study harder.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.