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...