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:
- 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
- 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
- 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