Exception class,

Source: Internet
Author: User

Exception class,
I. Exceptions

1. Exception Overview
Exception Overview
An exception is an error that occurs when the Java program is running.
2. Abnormal Inheritance System
Throwable
Error
Exception
RuntimeException

 

Ii. Differences between exceptions during compilation and Runtime

1. Differences between exceptions during compilation and Runtime
Exceptions in Java are classified into two categories: compile-time exceptions and runtime exceptions.
All RuntimeException classes and their subclass instances are called runtime exceptions. Other exceptions are compilation exceptions.

Exception during compilation
The Java program must be displayed for processing; otherwise, an error occurs in the program and compilation fails.
Runtime exception
No display processing is required, and the exception can be handled in the same way as during compilation.

Iii. Several Common Throwable Methods

A: getMessage ()
Obtains the exception information and returns a string.
B: toString ()
Obtains the exception class name and exception information, and returns a string.
C: printStackTrace ()
Obtain the exception class name and exception information, and the location where the exception occurs in the program. Return Value void.

 

 

Iv. Difference between throws and throw

Throws

Used after the method declaration, with the exception class name
Can be separated with multiple exception class names by commas
Indicates that an exception is thrown and is handled by the caller of this method.

Throw

Used in the method body with the exception Object Name

Only one exception object name can be thrown

Throws an exception and is processed by the statement in the method body.

V. finally features

A: finally features

The finally controlled statement body "must" be executed (even if there are return statements in try and catch)
Special case: the jvm exits before it is executed to finally (for example, System. exit (0 ))
B: Role of finally
This API is used to release resources and will be seen in IO stream operations and database operations.
C: A return Statement exists in catch, and the finally statement is also executed. Because finally is generally used to close resources, code in the finally block must be executed before return, except in special cases.

 

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.