Go Java exceptions

Source: Internet
Author: User
Tags try catch

Abnormal

An exception is a flow of instruction that causes a program to break execution. In Java, there is also a clear definition of exceptions in the API, called exception classes.

    • ERROR:JVM error, no processing in the program, to the virtual machine.
    • Exception: An exception that occurs in a program that can be processed in a program. Exception is also divided into compile-time exceptions, run-time exceptions.
    • Compile-time exception: compilation fails after code is written. Example: ParseException (Conversion exception), ClassCastException type conversion exception
    • Run-time exception: the code itself is not a problem, can be compiled, run time error. For example: NullPointerException (null pointer exception), ArithmeticException (Math exception, 0 divisor), ArrayIndexOutOfBoundsException (array corner bounds exception)
Exception handling

1. Catch exception: Try catch directly handles possible exceptions 2. Declaring exceptions: The throws declaration tells a possible exception in the call, exposes the problem, and the caller handles the

try{        //可能出现异常的代码  }catch(Exception1 e1){        //处理的方式1        e.printStaticTrace();  }[catch](Exception2 e2{ //处理的方式2}[finally]{ //一定要执行的代码 } 
    • Finally: abnormal consent exits, regardless of whether the program produces an exception, the contents of the finally statement block are bound to be executed.
    • Throws keyword: Used to declare a method, which means that this method does not handle the exception, but instead throws it to the call, who calls the method, and who handles the exception.
    • Throw keyword: Normally, exceptions are thrown by the JVM, and in addition to this, we can throw an exception artificially by the Throw keyword.
Several ways to combine try,catch,finally:
    1. Try,catch,finally
    2. Try,catch
    3. Try,finally

Go Java exceptions

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.