Java Basic Learning 20180322-Exception __java

Source: Internet
Author: User

1. Exceptions in the program
(1) in the process of running the program, the accident occurred, deviation from the intention of our program itself, can be understood as abnormal.
(2) Using the exception mechanism in Java, we can improve the robustness of the program.
(3) in Java, a variety of different exception types are described by Throwable and their subclasses.

2. Type of exception

3. Exception Handling
In Java applications, the exception handling mechanism is: Throw an exception, catch an exception
Implemented with 5 keywords: try, catch, finally, throw, throws

4.try-catch-finally
Experience and summary in practical application
(1) When handling the abnormal operation, the logic is used to avoid the auxiliary try-catch processing.
(2) After multiple catch fast, you can add a catch (Exception) to handle the exceptions that may be missed.
(3) For uncertain code, you can also add try-catch to deal with potential anomalies.
(4) Try to handle the exception, avoid simply call Printstacktrace () to print output
(5) How to deal with the exception, according to different business needs and the type of exception to decide
(6) Try to add the finally statement to release the resources that are occupied

5.throw, throws
You can declare the type of exception that will be thrown through the throws and throw the resulting exception by throw
If a method may have an exception, but is incapable of handling the exception, you can throw an exception with the throws clause at the method declaration

6.throw Rules of Use
When a subclass overrides a method that the parent class throws an exception, the declared exception must be the same class or subclass of the exception declared by the parent class method

7.throws Rules of Use
When a subclass overrides a method that the parent class throws an exception, the declared exception must be the same class or subclass of the exception declared by the parent class method

8. Custom Exceptions
Custom exceptions can be used to describe the types of exceptions generated by a particular business
The so-called custom exception, is to define a class, to inherit the Throwable class fire its subclass

9. Abnormal chain
When you catch an exception and throw another exception, if you want to send the cause of the exception to a string, that is, the underlying exception information to the upper layer, it formed an abnormal chain

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.