Java Fundamentals-Exception system

Source: Internet
Author: User
Tags finally block throwable

    • In Java, exception objects are derived from an instance of the Throwable class, as shown in the Java exception System:

All exceptions are inherited by Throwable, and the next layer is immediately decomposed into two branches,Error and Exception.

    • Error: Describes an internal error and resource exhaustion error for the Java Runtime system. Generally refers to virtual machine related problems, such as system crashes, virtual machine errors, etc., such errors can not be recovered or impossible to capture, will lead to application interruption , usually not processed . Because if such an internal error occurs, there is nothing more to do than advertise the user and try to make the program safely terminated.
    • Exception Exceptions : Java exceptions are divided into two types,checked Exception(compile-time exceptions) and runtimeexception(run-time exceptions).
  1. checked Exception (compile-time exception): Java considers checked Exception to be an exception that can be handled during the compile phase, typically IOException and SqlException . So it forces the program to handle all checked Exception , and RuntimeException does not need to be processed,
  2. runtimeexception(Runtime exception): These exceptions are usually caused by a program logic error, which is usually the programmer's fault , indicating that the program has a bug, So, generally do not need to do exception handling , directly let the program stop, the code is modified by the caller. Common Nullpointexception,indexoutofboundsexception (subscript out-of-bounds exception), Classcastexception,arithmeticexception (Arithmetic operation exception, For example, a divisor of 0, and so on), Arraystoreexception (an exception that holds an incompatible object with the declaration type in the array), Negativearraysizeexception (creates an array error exception with a negative size), NumberFormatException (number format exception), etc.
    • auto Throw the exception all the way to the top (without the programmer throwing it in the code) and always encounter the processing code. If there is no processing block, to the topmost level, if it is multithreading is thrown by Thread.run (), if it is a single thread is thrown by main (). Once thrown, the thread exits if it is a thread. If the exception is thrown by the main program, then the entire program exits. Runtime exceptions are subclasses of the exception, and there are general exceptions that can be handled by catch blocks. It's just that we're not dealing with him. That is, after a run-time exception occurs.                          ,         &NB Sp                          ,         &NB Sp                      
    • If you do not want to terminate, you must catch all run-time exceptions and never let the processing thread exit. Abnormal data in the queue, the normal processing should be to discard the abnormal data, and then log . The handling of normal data should not be affected by abnormal data. This can be a good application in this scenario, but it does not mean that you should do so in all scenarios. If, in other scenarios, you encounter some errors, if you exit the program better, then you can ignore the runtime exception, or by the exception of the handling of explicit control program exit.

    • Java exception handling methods are: throw exceptions , catch exceptions . Mainly depends on the try, catch, finally, throw, throws five keywords.

  1. try: Place code inside it that might throw an exception

  2. Catch: The following corresponds to the exception type and a block of code that indicates that the catch block is used to handle this type of code block and can have multiple catch blocks.

  3. finally: primarily used to reclaim material resources (such as database connections, network connections, and disk files) opened in a try block, the exception mechanism always guarantees that the finally block is always executed . Only after the finally block is executed does the return or throw statement in the try or catch block be returned, and if finally the statement of the terminating method, such as return or throw, is used, the execution is not skipped and stopped directly.

  4. throw: Used to throw an actual exception that can be used as a statement, throwing a specific exception object.

  5. throwsused in method signatureThat declares an exception that the method might throw
    1 Importjava.io.IOException;2 3  Public classTest {4     5      Public Static voidMain (string[] args) {6SYSTEM.OUT.PRINTLN ("Returning result is" +testexception ());7     }8     9      Public Static inttestexception () {Ten         inti = 1 ; One         Try{ A             Throw NewIOException ();  -}Catch(Exception e) { -System.out.println ("Catch it!")); the             returni; -}finally{ -i = 2 ; -System.out.println ("Finally OK") ; +System.out.println ("i =" +i); -             //return i; +         } A     } at}

    When the return statement on Line 20th is commented out, the result of the output is

    Catch it!
    Finally OK
    i = 2
    returning result is 1 It also indicates that if there is a return statement in a try or catch, the intermediate cache variable mechanism of Java has cached the results returned in a try or catch before entering finally, and after executing the statement in Finally, Return the cached result directly, and when the return statement is in finally, return the current result directly.
    When the return statement of Line 20th is not commented out, the result of the output is

    Catch it!
    Finally OK
    i = 2
    Returning result is 2

Java Fundamentals-Exception system

Related Article

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.