Have return if it is a try Catch finally Run command

Source: Internet
Author: User
Tags finally block try catch

Background:

Yesterday a friend went out to interview and encountered the question: "C # catch there return." Finally also get it run? ”

Personal Summary Practice:

1, whether there is an abnormal wood. The code in the finally block will run .
2. finally will still run when there is a return in the try and catch .

The detailed case ratio is as follows (this is verified with a function with no return value):

3, Assuming that the value is passed. The value changed in finally has no effect on the value returned by return in the try or catch block . The assumption is that reference type parameters (address passing or object), and the value change in finally will have an effect on return .

Detailed case:

Reference case--array type:


Value Case--int Type:


Thinking: String types are reference types. So is the return value affected by finflly? Let's take a look at the case.-----In fact, the string behaves like the int type without being affected by finally:


Think: So what's the effect of adding return to finally? There is a picture of the truth:


Visible. Finally, the return is not agreed. Or you can't even compile.


Copyright notice: Author: jiankunking Resources: http://blog.csdn.net/jiankunking This document is copyrighted to the author and csdn partner, welcome reprint, however, if not announced from this section the permissions must be retained. and connect to the original article page to give a view of the location.

Have return if it is a try Catch finally Run command

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.