The absolute robustness of program Try-catch nesting

Source: Internet
Author: User

In the process of writing the program, we are familiar with the Try-catch, catch the exception to deal with, to ensure the robustness of the program.

Today's sudden thought, what if the code in our catch is abnormal? Let's make one of the following assumptions

static void Main (string[] args) {    try{        //code A    }    catch{        //code B    }    finally{        //code C    }}

Following our usual use of code A, we may end up with code B error handling and possibly write the error log at code C.

So the question is, what if we're out of order because of some unpredictable factor in code B or code C? (although I have not encountered this situation, haha)

Then the programmer realizes the following structure with a dead-minded attitude

static void Main (string[] args) {    try{        //code A        try{            //code B        }        catch{            //code C        }        finally{            //code D        }        //code E    }    catch{        //code F    }    finally{        //code G    }}

According to this structure, we do not write any code in the position of code F and G (not always an exception?). or write the absolute error-free code, the subject code is in the location of line B, C's location exception handling, D's location to write the log.

So, even if catch and finally are abnormal, they will be caught by the outer catch, so is it absolutely robust?

The absolute robustness of program Try-catch nesting

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.