In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how many situations are there when java throws an exception". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how many situations are there when java throws an exception?"
1. Compilation error / / code 1public static void test () throws Exception {throw new Exception ("parameter out of bounds"); System.out.println ("after exception"); / / compilation error, "inaccessible statement"} 2. You can execute / / code 2try {throw new Exception ("parameter out of bounds");} catch (Exception e) {e.printStackTrace ();} System.out.println ("after exception"); / / you can execute 3. Throw an exception and do not execute / / code 3if (true) {throw new Exception ("parameter out of bounds");} System.out.println ("after exception"); / / throw exception and will not execute
Summary:
If an exception is thrown before a piece of code and the exception is not caught, the code will produce a compile-time error "inaccessible statement". Such as Code 1
If an exception is thrown before a piece of code, and the exception is try... Caught by catch, if no new exception is thrown in the catch statement at this time, this code can be executed, otherwise, it is the same as section 1. 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 those of the RuntimeException class and its subclasses, and are unchecked exceptions, such as NullPointerException, IndexOutOfBoundsException, and so on. Because such exceptions are either system exceptions and cannot be handled, such as network problems
Either it is a program logic error, such as a null pointer exception; JVM must stop running to correct this error, so the runtime exception can be handled without handling (caught or thrown up, of course), but by JVM itself. Java
Runtime automatically catch to the RuntimeException of the program throw, then stops the thread and prints the exception.
Non-runtime exceptions are exceptions other than RuntimeException, which belong to the Exception class and its subclasses in type, and are checked exceptions. Non-runtime exceptions must be handled (caught or thrown up), and if not handled, the program will have a compilation error. In general, the Exception that says throws in API is not RuntimeException.
Common runtime exceptions:
Common non-runtime exceptions:
Exception problem in Java (question of whether to continue execution after exception is thrown) public static void test () throws Exception {throw new Exception ("parameter out of bounds"); System.out.println ("after exception") / / compilation error, "inaccessible statement"} / / Code 2ax / exception is caught, the log prints the exception, and the code continues to execute try {throw new Exception ("parameter out of bounds");} catch (Exception e) {e.printStackTrace ();} System.out.println (after exception) / / you can execute / / psvm shortcut public static void main (String [] args) {try {test ();} catch (Exception e) {e.printStackTrace ();}} public static void test () throws Exception {/ / Code 3 if (true) {throw new Exception ("parameter out of bounds") } System.out.println ("after exception"); / / throw an exception and will not execute} public static void test () throws Exception {/ / Code 4 try {int iTunes 1ap0;} catch (Exception e) {e.printStackTrace (); throw new Exception ("print and throw exception hints after code executes an exception") } System.out.println ("after exception") / / throw an exception Will not print the log java.lang.ArithmeticException: / by zero at zmc.eter.etern.text.text.test (text.java:23) at zmc.eter.etern.text.text.main (text.java:14) java.lang.Exception: print after the code executes an exception and throw an exception prompt at zmc.eter.etern.text.text.test (text.java:26) at zmc.eter.etern.text.text.main (text. Java:14) these are all the contents of the article "how many situations are there when java throws an exception?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.