Talk about the error and exception in Java

Source: Internet
Author: User

The connection between error and exception
    • Inheritance structure: Both error and exception inherit from Throwable,runtimeexception inherited from exception.

    • The error and runtimeexception and their subclasses are called unchecked exceptions (unchecked exception), and other exceptions become checked exceptions (Checked exception).

The difference between error and exception
    • The error class generally refers to problems associated with virtual machines, such as system crashes, virtual machine errors, insufficient memory space, overflow of method call stacks, and so on. such as Java.lang.StackOverFlowError and Java.lang.OutOfMemoryError. For such errors, the Java compiler does not check them. For this kind of error caused by the application interruption, only by the program itself cannot be recovered and prevented, encountered such errors, it is recommended to let the program terminate.

    • The exception class represents exceptions that the program can handle and can be captured and possibly recovered. If you encounter such an exception, you should handle the exception as much as possible so that the program resumes running, rather than terminating the exception arbitrarily.

Run-time exceptions and checked exceptions

Exception is also divided into runtime exceptions (runtime Exception) and checked exceptions (Checked Exception).

    • RuntimeException: It is characterized by the Java compiler does not check it, that is, when the program can occur in such an exception, even if it is not captured with Try......catch, is not used throws thrown, or will be compiled through, such as ArithmeticException with a divisor of zero, wrong type conversions, array out-of-bounds access, and attempts to access null pointers. The principle of dealing with runtimeexception is that if a runtimeexception occurs, it must be a programmer's fault.

    • Checked exceptions (IOException, etc.): This kind of exception if there is no try......catch or throws thrown, compile is not pass. Such exceptions are generally external errors, such as files not found, attempts to read data from the end of the file, and so on, which is not an error in the program itself, but an external error that occurs in the application environment.

What is the difference between a throw and a throws two keywords
    • Throw is used to throw arbitrary exceptions, you can throw arbitrary throwable, including custom exception class objects; throws always appears in a function header to indicate the various exceptions that the member function might throw. If the method throws an exception, the method is called to handle the exception.
Try-catch-finally-return Execution Order
    • 1, regardless of whether there is an exception, the code in the finally block will be executed;
    • 2. When there is a return statement in the try and catch, the finally block will still execute;
    • 3. Finally is executed after the expression operation after return, so the function return value is determined before finally execution. Regardless of the code in the Finally, the returned value will not change, still is the value saved in the previous return statement;
    • 4, finally, it is best not to include return, or the program will exit early, the return value is not a try or catch in the saved return value.

Example:

Case 1:try{} catch () {}finally{} return;
Execute in normal order.

Case 2:try{return;} catch () {} finally{} return;
The program executes code in the try block before return (including expression operations in the return statement);
Then execute the finally block and finally execute the return in try;
The return statement after the finally block is no longer executed.

Case 3:try{} catch () {return;} finally{} return;
The program executes the try first, and if an exception is encountered to execute a catch block,
There are exceptions:
Executes the code in the catch before return (including the expression operation in the return statement), and then executes all the code in the finally statement,
Finally executes the return in the catch block. The return statement after the finally block is no longer executed.
No exception:
Executes the try again and finally executes the last return statement.

Case 4:try{return;} catch () {} Finally{return;}
The program executes code in the try block before return (including expression operations in the return statement);
Then execute the finally block, because there is a return in the finally block and exit early.

Case 5:try{} catch () {return;} Finally{return;}
The program executes the code in the Catch block before return (including the expression operation in the return statement);
Then execute the finally block, because there is a return in the finally block and exit early.

Case 6:try{return;} catch () {return;} Finally{return;}
The program executes code in the try block before return (including expression operations in the return statement);
Exception: Executes the code in the Catch block before return, including the expression operation in the return statement;
Then the finally block is executed because there is a return in the finally block and exits early.
No exception: Then the finally block is executed, because there is a return in the finally block and exits early.

Test procedure Case

public class Finallytest  {public    static void Main (string[] args) {        System.out.println (new Finallytest (). Test ());;    }    static int test ()    {        int x = 1;        Try        {            x + +;            return x;        }        Finally        {            ++x;}}    }

The print result is 2.

Based on the previous analysis, in the TRY statement block, when the return statement is executed, the results to be returned are ready, and then the program goes to finally execution. Before you go, try to put the results to be returned in a different local variable from the X, after the finally, and then take out the return results, so even if the variable x in finally is changed, it will not affect the return result.

Original link: http://blog.csdn.net/goodlixueyong/article/details/47122487

Talk about the error and exception in Java

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.