Java entry (4)-Exception Handling

Source: Internet
Author: User

In writing. net Program, seldom pay attention to exception handling. at that time, windows or Web applications were used, and exceptions were basically handled on the interface. the only processing is to use exception handling during database operations. make yourself feel. net does not seem to have to handle exceptions. of course this is wrong.

When learning Java, I read the exception handling in Java and found that exception handling is still important.

 

1: What is an exception:

An exception occurs when the program runs. this is not an exception during compilation and debugging. set exception handling to prevent unexpected errors and stop the program.

 

Throw exception: if an exception occurs during Java program execution, an exception object is generated. This object is instantiated by Java and handed over to Java for running. This is throw exception.

Catch exception: When the runtime receives an exception object, it looks for code that can handle this exception. This is called catch exception.

Exceptions thrown by every method in Java are described in the JDK documentation. We only need to capture this exception, but not all exceptions need to be captured and handled.


2: exception type


Error: error, which is generated and thrown by the Java Virtual Machine and is not processed by the program.

Exception: the parent class of all Exception classes, which must be explicitly declared and captured by the user.

Runtime exception: a special type of exception, such as Division 0 and subscripts out of bounds. It is complicated to handle. Generally, it is handled by the default exception handler of the system (the program does not handle the exception)


3: How can an exception be handled.

Try {

// Statements that may throw exceptions

} Catch (execption1e ){

Processing

} Catch (execption2 e ){

Processing

} Finally {

What to do last (must be executed)

}

Explanation:

A: Try Code contains statements that may generate exceptions. Try code can be followed by one or more catch statements. Each catch statement then declares the specific exceptions it can handle and provides a processing method.

B: When an exception occurs, the program terminates execution and runs the code in the catch according to the error type.

C: The finally statement must be executed no matter whether an exception exists.


4: custom exception

Java can also customize exceptions as follows:

A: declare your own exception class by inheriting the java. Lang. exception class.

B: generate an instance with a custom exception in the appropriate location of the method and throw it with throw.

C: In the declaration part of the method, use throw to declare the exceptions that may be thrown by the method.

 

Java Exception Handling principles

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

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

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


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 is an important aspect in software development and test script development using the Java language. The importance of exception handling is that the code you developed is more robust and stable.

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.