Try_catch_finally application (from larrysunday)

Source: Internet
Author: User
Tags finally block

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. IO;

Namespace test_try_catch_finally
{
Class Program
{
Private Static bool openfile ()
{
Bool ret = false;
Try
{
Try
{
File. Open (@ "C: \ WINDOWS \ notepad. X", filemode. Open );
Ret = true;
}
Catch (exception ex)
{
Throw ex;
Console. writeline ("exception: {0}", Ex. Message );
Ret = true;
}
Finally
{
Console. writeline ("finally ");
Ret = true;
}
Console. writeline ("RET: {0}", Ret. tostring ());
Return ret;
}
Catch (exception ex)
{
Console. writeline ("exception: {0}", Ex. Message );
Ret = false;
}
Console. writeline ("RET: {0}", Ret. tostring ());
Return ret;
}

Static void main (string [] ARGs)
{
Console. writeline ("RET: {0}", openfile ());
Console. readkey ();
}
}
}

++ ++ ++
AboveCodeThe test result shows that the execution sequence is worth recording:

First, the underlying order is: file in try. open (@ "C: \ WINDOWS \ notepad. X ", filemode. open); after an error is reported, jump into the bottom-layer catch block and execute throw. Then, immediately jump into the bottom-layer Finally block and execute the Catch Block. After the upper-layer catch is executed, finally, execute the console after the upper Catch Block. writeline ("RET: {0}", Ret. tostring (); return ret;

------------------------ As a result, the function call is completed.

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. IO;

Namespace test_try_catch_finally
{
Class Program
{
Private Static bool openfile ()
{
Bool ret = false;
Try
{
Try
{
File. Open (@ "C: \ WINDOWS \ notepad. X", filemode. Open );
Ret = true;
}
Catch (exception ex)
{
Return false;
Throw ex;
Console. writeline ("exception: {0}", Ex. Message );
Ret = true;
}
Finally
{
Console. writeline ("finally ");
Ret = true;
}
Console. writeline ("RET: {0}", Ret. tostring ());
Return ret;
}
Catch (exception ex)
{
Console. writeline ("exception: {0}", Ex. Message );
Ret = false;
}
Console. writeline ("RET: {0}", Ret. tostring ());
Return ret;
}

Static void main (string [] ARGs)
{
Console. writeline ("RET: {0}", openfile ());
Console. readkey ();
}
}
}

In the lower-layer catch, there is return false; before throw ex;, return false is completed in the catch; and then the bottom-layer Finally block is directly executed, the function is called...

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.