Java throws an exception--does the subsequent code execute

Source: Internet
Author: User
Tags java throws

Recently wrote an interface program, the main program in the calling interface program, the program error throws an exception, and return null value, after parsing the code, feel that since the exception is thrown, it should not have the back of the program's return value of the program continue to execute. Later, after testing and verification, and from the Internet to find the answer, the exception is thrown, follow-up procedures continue to implement a new understanding.

Because there is an article written on the Internet is really more detailed, here directly copied to prepare for the record.

Cited article address: https://www.cnblogs.com/wangyingli/p/5912269.html

 //  code 1   Public  static  void  test () throws   Exception {      Throw  new  Exception ("parameter Out of Bounds"  "after exception"); //  compilation error, "unreachable statement" } 
// Code 2 Try {    thrownew Exception ("parameter Out of Bounds");} Catch (Exception e) {    e.printstacktrace ();} System.out.println ("after exception"); // can perform
// Code 3 if (true) {    thrownew Exception ("parameter Out of Bounds");} SYSTEM.OUT.PRINTLN (// throws an exception, does not execute

Summarize:

    1. If an exception is thrown before a piece of code, and the exception is not captured, the code generates a compile-time error "unreachable statement." such as code 1
    2. If an exception is thrown before a piece of code, and the exception is caught by Try...catch, if the catch statement does not throw a new exception, then this code can be executed, otherwise, the same as 1th. such as Code 2
    3. If an exception is thrown in a conditional statement, the program can be compiled, but subsequent statements will not be executed. such as code 3

Also summarize the differences between run-time exceptions and non-run-time Exceptions:

    • Runtime exceptions are exceptions to the RuntimeException class and its subclasses, and are non-subject exceptions, such as NullPointerException, Indexoutofboundsexception, and so on. Because such exceptions are either system exceptions or cannot be handled, such as network problems;
      It is either a program logic error, such as a null pointer exception, and the JVM must stop running to correct this error, so the runtime exception can be handled by the JVM itself without processing (capturing or throwing up, and of course, processing). Java runtime automatically catch the runtimeexception of the program throw, then stop the thread and print the exception.

    • A non-run-time exception is an exception that is runtimeexception except that the type belongs to the exception class and its subclasses. Non-runtime exceptions must be processed (caught or thrown up), and if not handled, the program will have a compilation error. In general, the API is written throws exception are not runtimeexception.

Common run-time exceptions:

Common non-run-time Exceptions:

Java throws an exception--does the subsequent code execute

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.