Java exception handling mechanism is interesting.

Source: Internet
Author: User
Tags try catch

Foreword : On the network to see an "in-depth understanding of Java exception Handling mechanism", read feel oneself also have a little idea, indeed in many Java Scholar's eyes, exception handling mechanism is try catch Finally, what good understanding, many times, I also often ignore this aspect of content, feel nothing interesting, then let's have a little horse.

1. Modification of the original author's example

Package Mwq;public class T1 {public static void main (string[] args) {Boolean b = true;try {b = tb1 ();} catch (Arithmeticex Ception e) {System.out.println ("main catch the value of B:" + b); b = False;throw e;} finally {System.out.println ("main finally B:" + B);}} public static Boolean tb1 () throws ArithmeticException {Boolean b = true;try {if (!TB2 ()) {return false;}} catch (Arithmet Icexception e) {System.out.println ("Tb1 catch the value of B:" + b); b = False;throw e;} finally {System.out.println ("tb1 finally B:" + b);} return b;} @SuppressWarnings ("finally") public static Boolean TB2 () throws ArithmeticException {Boolean b = true;try { System.out.println (1/0);} catch (ArithmeticException e) {System.out.println ("Tb2 catch the value of B:" + b); b = False;throw e;} finally {System.out.println ("TB2 finally B:" + b); return b;}}}
2. Examples of what I feel interesting
Package Mwq;public class T {public static void main (string[] args) {try {System.out.println (1/0); System.out.println ("CCCCCCCCCCCCCCCCCCC");} catch (ArithmeticException e) {System.out.println (1/0); System.out.println ("Aaaaaaaaaaaaaaaaaaa");} finally {System.out.println (1/0); System.out.println ("bbbbbbbbbbbbbbbb");}}}
The first example is not much to say, if your answer is as follows

TB2 catch the value of B:TRUETB2 finally B:FALSETB1 finally B:truemain finally B:false
Then, you don't need to see the original author's example, but you can read his basic theoretical knowledge.

For the second example, in a lot of practical applications in a lot of, normal process, like a try statement, in most cases, we consciously add the corresponding catch statement, or at least catch Exception, then this is no problem, but many times, Our Catch statement also has some logic processing, not a simple output error log, if we are in the catch again error, many times, we think most of the catch is normal code, so we no longer catch in the catch capture, then according to the above example, Is that the answer to your question?

Exception in thread "main" java.lang.ArithmeticException:/by Zeroat Mwq. T.main (t.java:13)
Error when the code of 13 lines, why is not the 10th line of code error, because the role of finally is, whether catch or not catch error, finally to execute, although the code of the 10th line has thrown an exception, However, the program executes finally instead of printing the catch error on the stack, because finally the error is in the end, the program must first handle the errors in the Finally, then the program will print out the error, then terminate, this time the 10th line of error will not be output, If this is the procedure,

Package Mwq;public class T {public static void main (string[] args) {try {System.out.println (1/0); System.out.println ("CCCCCCCCCCCCCCCCCCC");} catch (ArithmeticException e) {System.out.println (1/0); System.out.println ("Aaaaaaaaaaaaaaaaaaa");} finally {System.out.println ("bbbbbbbbbbbbbbbb");}}}
So what do you think the result will be, and there are two kinds of results?

Bbbbbbbbbbbbbbbbexception in thread "main" java.lang.ArithmeticException:/by Zeroat Mwq. T.main (T.JAVA:10)

Exception in thread "main" java.lang.ArithmeticException:/by Zeroat Mwq. T.main (t.java:10) bbbbbbbbbbbbbbbb
Why there are two kinds of results, most of the time, will be output bbbbbbbbbbbbbbbbb, but because the out object is also the input and output stream, the JVM when processing, in rare cases will be the error message first printed out, Then output the bbbbbbbbbbbbbbb of the Out object, which may be the cause of thread scheduling, but ask the small partners to doubt my conclusion because I have not proved it.

Summary : Overall, I feel that the Java exception handling mechanism is interesting and not simple.






Java exception handling mechanism is interesting.

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.