Java Learning--Exception handling

Source: Internet
Author: User
Tags throwable

    使用 Java 语言进行软件开发和测试脚本开发时不容忽视的一个问题就是异常处。是否进行异常处理直接关系到开发出的软件的稳定性和健壮性。

Exception refers to the various conditions, such as: file cannot be found, network connection failure, illegal parameters and so on. An exception is an event that interferes with the normal instruction flow during the program's run.

Java describes various kinds of exceptions through the many subclasses of the Throwable class in the API. Thus, Java exceptions are objects, examples of throwable subclasses, and describe error conditions that occur in a piece of code. An error throws an exception when the condition is generated.


In Java, all exceptions have a common ancestor throwable (can be thrown). Throwable specifies the commonality of any problem that can be transmitted through a Java application using the exception propagation mechanism in the code.

    • Throwable:

There are two important subclasses: Exception (Exception) and error (Errors), both of which are important subclasses of Java exception handling, each of which contains a large number of subclasses.

    • Error (Errors that the program cannot handle)

Represents a more serious problem in running the application. Most errors are unrelated to what the code writer does, and represent a problem with the JVM (the Java Virtual Machine) that is running the code. For example, the Java Virtual machine runs the error (virtual Machineerror) and OutOfMemoryError occurs when the JVM no longer has the memory resources required to continue the operation. When these exceptions occur, the Java Virtual machine (JVM) typically chooses to terminate the thread.

These errors are not available because they are outside the control and processing power of the application and are mostly not allowed when the program is running. For a well-designed application, even if it does, it should not be an attempt to deal with the anomalies that it causes. In Java, errors are described by the subclasses of the error.

    • Exception (The exception that the program itself can handle)

Exception This exception is divided into two major classes of runtime exceptions and non-runtime exceptions (compilation exceptions). These exceptions should be handled as much as possible in the program.

 1. 运行时异常

are runtimeexception classes and their subclass exceptions, such as nullpointerexception (null pointer exception), indexoutofboundsexception (subscript out-of-bounds exception), and so on, which are not checked for exceptions, You can choose to capture or not handle the program. These exceptions are usually caused by a program logic error, and the program should avoid this kind of exception as much as possible from a logical point of view.

The characteristic of a line exception is that the Java compiler does not check it, that is, when such an exception may occur in the program, even if it is not captured with the Try-catch statement, it is not thrown with the throws clause declaration, and it is compiled.

     2. 非运行时异常 (编译异常)

is an exception other than RuntimeException, and the type belongs to the exception class and its subclasses. From the point of view of the program syntax is the exception that must be handled, and if not handled, the program cannot be compiled through. such as IOException, SqlException, and user-defined exception exceptions, generally do not customize check exceptions.

Principles of Java exception handling
    • Handling exceptions as much as possible

To handle exceptions as much as possible, consider declaring exceptions if the condition does not allow you to complete processing in your own code. If a person is to avoid handling exceptions in code, it is a false and dependent practice to make declarations only.

    • Specific problem-specific solutions

Part of the advantage of exceptions is the ability to provide different processing operations for different types of problems. The key to effective exception handling is to identify specific fault scenarios and develop specific behaviors that address this scenario. To take advantage of exception handling capabilities, you need to build specific processor blocks for specific types of problems.

    • Log exceptions that may affect the application's running

At a minimum, take some permanent steps to document the exceptions that may affect the operation of the application. Ideally, of course, it is the first time to resolve the underlying problem that caused the exception. However, regardless of the processing operation, the underlying critical issues should generally be documented. Although this is a simple operation, it can help you to keep track of the causes of complex problems in your application with little time.

    • Convert an exception to a business context based on the situation

To notify an application-specific issue, it is necessary to convert the application to a different form. Code is easier to maintain if the exception is represented by a business-specific state. In a sense, whenever an exception is passed to a different context (that is, another technology layer), the exception should be converted to a form that is meaningful to the new context.

Java Exception Handling

Managed by 5 keyword try, catch, throw, throws, finally. The basic process is to wrap the statement that you want to monitor with a try statement block, and if an exception occurs within a try statement block, the exception is thrown, your code can catch the exception in the Catch statement block, and the exception that is generated by some system is automatically thrown in the Java runtime.

You can also declare the method to throw an exception by using the throws keyword on the method, and then throw the exception object through a throw inside the method. The finally statement block executes before the method executes a return, and the general structure is as follows:

try{  程序代码 }catch(异常类型1 异常的变量名1){  程序代码 }catch(异常类型2 异常的变量名2){  程序代码 }finally{  程序代码 }

A catch statement can have more than one exception, which matches more than one of the above, and executes the CATCH statement block only when the exception on the match is executed.

The type of catch is defined in the Java language or is defined by the programmer itself, indicating that the code throws an exception, the exception variable name represents the reference to the object that throws the exception, and if the catch catches and matches the exception, then the exception variable name can be used directly. At this point the exception variable name points to the matching exception and can be referenced directly in the catch code block. This is very, very special and important!


The purpose of Java exception handling is to improve the robustness of the program, is a major feature of the Java language, but also a difficult point, master exception processing can make the code written more robust and easy to maintain. And more importantly, the idea of Java handling exceptions is particularly worthy of our study, we must have a long-term vision, with a holistic view, the future of the mistakes to be killed in the cradle. Of course, Rome cold day, we still need to study harder.

Java Learning--Exception handling

Related Article

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: 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.