Java learning notes (Exception Handling)

Source: Internet
Author: User
Tags finally block

1) Exception Handling Mechanism
During the program running, if an unexpected statement (such as Division-free error or IO error) occurs, the Java Virtual Machine will stop and will not continue to execute subsequent statements, instead, the exception handling module is located from the inside out (Because exception capture can be nested within a method:
A) finally find the exception handling module, and then execute the statements in the Exception Handling Module. After the exception handling statement is executed, continue to execute the subsequent statements of the Exception Handling Module (possibly the finally module ), as follows:

Public class exceptiontest {public static void main (string [] ARGs) throws exception {try {callsomemethods. doa1 ();} catch (effectiona e) {// todo auto-generated catch blockhandlingsomething. printmsg (E. getmessage (); // A2-7} system. out. println ("execution completed! "); // A1-7, A2-8} Class Partition tiona extends exception {public partition tiona () {super (" partition tiona ");} public partition tiona (string MSG) {super (MSG) ;}} class exceptiona1 extends tiona {public exceptiona1 () {super ("exceptiona1") ;}} class exceptiona2 extends tiona {public exceptiona2 () {super ("exceptiona2") ;}} class callsomemethods {public static void doa1 () throws exception {// 1 Exception Handling Code module start try {// 2 exception capturing code block start t Ry {// The exception to be tested. Put the statement that generates the exception in front of handlingsomething. doa3 (); // A3-1HandlingSomething.DoA2 (); // A2-1HandlingSomething.DoA1 (); // A1-1} catch (exceptiona1 e) {handlingsomething. printmsg (E. getmessage (); // A1-2} catch (effectiona e) {// here the new throw is just to demonstrate that the Finally block will be executed. // continue to throw handlingsomething. printmsg ("continue running 2"); // A2-2throw new worker tiona (E. getmessage () + ": continue running 2"); // A2-3} finally {handlingsomething. printmsg ("This will always execute 1" ); // A1-3, A2-4, A3-2} // 2 exception capture code block end // 2 exception subsequent statement, if the preceding exception is processed and no new exception is thrown, It will be executed. System. out. println ("this is the subsequent statement of exception capture processing module 2"); // A1-4} catch (javastiona e) {// continue to throw handlingsomething. printmsg ("continue running 1"); // A2-5throw new worker tiona (E. getmessage () + ": continue running 1"); // A2-6} finally {handlingsomething. printmsg ("here will always execute 2"); // A1-5, A2-7, A3-3} // 1 Exception Handling Code module end // 1 exception subsequent statement, if the preceding exception is processed and no new exception is thrown, It will be executed. Handlingsomething. printmsg ("this is the subsequent statement of exception capture Processing Module 1"); // A1-6} class handlingsomething {// here for demonstration, therefore, only the public static void doa1 () throws exceptiona1 {Throw new exceptiona1 () ;}// for demonstration, only the public static void doa2 () exception is thrown () throws exceptiona2 {Throw new exceptiona2 ();} public static void doa3 () throws exception {Throw new exception ("this exception is not handled! ");} Synchronized public static void printmsg (string MSG) {simpledateformat Thea = new simpledateformat (" yyyymmddhhmmss "); system. out. println (MSG + ":" + Thea. format (New Java. util. date ()));}}

B) If no exception handling block is found, it will be processed by the Java Virtual Machine. Whether or not the program will be terminated depends on the specific program and the specific exception, the above test will end the program.
The exception handling mechanism of Java and C # is also a typical responsibility chain model.
2) capture method
Use the try block to include code that may generate exceptions, followed by the exception matching processing block. There can be multiple exception matching processing blocks. Note:
Exception matching is performed in order. After a matched exception processing block is found, the subsequent matching will discard the matching. Therefore, when writing a matching block, in the exception inheritance system, the lower the hierarchy,
The higher the level is. Because the exception match is not precise match priority, but the matching order priority, the matching only needs to satisfy the "yes" relationship, that is, if an exception class is A, then a and a's
All sub-exception classes can be matched where exception class A matches.

3) comparison:
Compared with C #, Java's exception handling mechanism is more rigorous. It requires you to either handle the possible exception a or explicitly declare to the caller that you have not handled the Exception, otherwise, the Java compiler reports a compilation error.
Of course, everything has two sides. It is certainly a good thing to ensure that exceptions are correctly handled during the compilation period. However, this mechanism will also cause some problems. First, if the call relationship is complex and there are many exceptions, the code looks less pure,
It will be slightly messy; second, it will have high requirements on programmers, especially when writing the underlying code that involves transactions, programmers must be very cautious about exception handling, the transaction mechanism called by the upper layer cannot be blocked. Unless necessary, the exception in the Code involving the transaction mechanism should be thrown to the caller for handling as much as possible.

 

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.