javase--anomaly Mechanism

Source: Internet
Author: User

exception mechanism

Exceptions are anomalies that occur when the program is running. Occurs during runtime, Java program in the runtime of the abnormal situation, at this time, Java is based on the idea of object-oriented to describe the abnormal phenomenon and encapsulation of objects. The origin of the anomaly: The problem is also a specific thing in real life, can also be described in the form of Java classes, and encapsulated into objects. The exception is actually the object that Java describes to the abnormal situation.

first, abnormal system

Thorwable is the base class for all exceptions, which can be divided into error and exception two according to the place of exception occurrence:

    • For serious, Java is described by the error class (typically a major problem such as: A running class does not exist or a memory overflow, etc.). For error, it is generally not written in a targeted code to handle it. Error occurs at the bottom of the system, and it tells JVM,JVM to tell the user. Error once there is no targeted processing, modify the code directly.
    • For non-critical, Java is described by the exception class. The exception is caused by the JVM and tells the caller that the exception can be handled using a targeted processing method.

The capture and processing status by exception can be divided into checked exception (checked anomalies) and unchecked exception (non-checked exception):

    • Checked exception (checked for exceptions), also known as non-runtime exceptions (exceptions other than runtime exceptions are non-runtime exceptions), run-time errors caused by factors other than code capabilities. The Java compiler forces programmers to have capture processing, such as common Ioexeption and SqlException. If you do not capture or throw claim processing, the compilation will not pass
    • Unchecked exception (not subject to check anomalies), including error and runtimeexception, such as Common NullPointerException, indexoutofboundsexception. For the Runtimeexception,java compiler, it is the programmer's discretion not to require exception capture processing or to throw a declaration.

Divide by run-time exceptions and non-run-time Exceptions:

    • Typical runtimeexception include NullPointerException, indexoutofboundsexception, IllegalArgumentException and so on.
    • Typical non-runtimeexception include IOException, SqlException and so on.

second, exception handling1. Catching Exceptions

Capture mechanism: try-catch-finally (1-n-1/1-0-1/1-n-0)

    • try{} statement block: Inside is the Java code to be detected, may throw an exception, or it may run normally.
    • catch (Exception type) {} BLOCK: When Java is running, the system receives an exception object thrown in a try block, looking to handle the exception catch block, noting that the exception type needs to match the exception that the try{} statement block might throw. You can have multiple catch blocks. Different exception types correspond to different processing codes.
    • finally{} statement block: Regardless of whether the system throws an exception, it will be executed, usually to release resources. In addition to the previous execution of System.exit (0)
2. Throwing Exceptions

Throw mechanism: Throw,throws

    • Throws: Used on the function, followed by the exception class, can be followed by more than one, to declare an exception, let the caller know the function of the problem may occur, and the caller can give a pre-processing mode
    • Throw: Used in the function, followed by the exception object, throw the specific problem object, execution to the throw function has ended, jump to the caller, the specific problem object is also thrown to the caller
    • Throws the principle: the function inside has the exception throws, then the function must declare, inside throws what function to declare what, declares the purpose is in order to let the caller handle, if the caller does not process then compiles the failure!
    • Special case: When an exception object of RuntimeException and its subclasses is thrown through a throw in a function, the exception class cannot be declared on the function! The purpose of the declaration is not to let the caller handle it, so that the caller's program stops . Throwing an exception does not declare that an exception occurs when the program stops, if the declaration of the exception caller can catch and handle the exception and then the program can continue to run, you need to be aware that the exception handling should not affect the subsequent code run

javase--anomaly Mechanism

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.