2.Java Abnormal Learning

Source: Internet
Author: User

Concept of 1.Java anomaly

    • Examples of exceptions
    • 1. Division is an example of an exception that needs to be caught, and the divisor may be 0
    • exception handling the basic process is as follows
    • Once an exception occurs, the program does not follow the original process to continue to run
    • A. Program throws an exception
        • try{
        • throw new Exception ();
        • }
    • B. Catching exceptions
        • catch (Exception e) {
          • //exception handler
        • }
    • c.finally code block
    • It is important to note that an exception cannot be thrown in a finally block.
        • finally{
          • //must be executed /em>
          • //unless There is system.exit (0) in the catch, the Java Virtual machine will be launched
        • }
        li>
    • Summarize:
    • 1. When an exception is thrown in the program, the program jumps out of the code that caused the exception in the program, the Java Virtual machine detects the catch block that handles the exception that matches the try keyword, and if found, the code in the catch block, then proceeds to execute the program, The code for the exception that occurred in the try block is not re-executed. If no catch block is found to handle the exception, then the finally block code is Executed.
    • 2. The run function of a thread cannot throw an exception (it can be captured and processed inside the function, but cannot be committed), and the exception of the run function is submitted to Uncaughtexceptionhandler for processing, and later changes will be described in detail
    • 3. The other functions of the run function can be submitted to the superiors by the exception thrown in the Function.

    • Structure of the exception
    • Figure 1: Anomaly Structure (from reference I)

    • Exception classification
    • 1.unckeckedexception--error (inaccurate, But the Error is actually checked and thrown at Runtime) and runtimeexception-------no replenishment is required in the program
    • 2.checkedException-----ioexception, classnotfoundexception, clonenotsupportedexception
    • The check here is not checked against the compiler
    • Common runtimeexception-nullpointerexception, Array out-of-bounds, string out-of-bounds, arithmetic error (a/b), parameter type error (integer.parseint ("abc"))

    • Throw an exception when declaring a method

    • public void OpenFile () throws ioexception{//code}
    • The exception that is thrown when the general function declares is chenckedexception
    • a subclass method that overrides a method of a parent class cannot throw more exceptions than the parent class method , so sometimes the method of the parent class is designed to declare an exception, but the code of the actual implementation method does not throw an exception, and the purpose is to make it easier for the subclass method to overwrite the parent class method when the exception can be thrown.

    • Cathch exception
    • Note: a try block can handle multiple types of exceptions (up to one catch module will be executed, not the same as Switch)
    • A logical try after processing multiple exceptions will error-the following code will error [catch subclass exception before catch parent exception]
    • Because the latter catch cannot be executed to
        • try{
        • Code
        • }catch (Exception E) {
        • }catch (runtimeexception E) {//error must not be executed to
        • }

    • Custom exceptions ----that is, inherit from the exception
    • The most important thing is actually the name of the Exception.
    • Class MyException extends exception{}
    • The default constructor
    • 1.super () invokes the constructor of the base class to initialize
    • 2.super (message)

Reference: Http://www.codeceo.com/article/java-exception-learn.html#0-tsina-1-56987-397232819ff9a47a7b7e80a40613cfe1

2.Java Abnormal Learning

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.