Why use Finally in Try … Catch

前端 未结 14 1996
鱼传尺愫
鱼传尺愫 2020-12-05 06:38

I see that the Finally in Try .. Catch will always execute after any parts of the execution of the try catch block.

Is it any different to

14条回答
  •  再見小時候
    2020-12-05 06:58

    Code with four radio buttons:

    • Return in TRY
    • Return in CATCH
    • Throw in CATCH
    • Finish CATCH

      private void checkFinally()
      {
          try
          {
              doFinally();
          }
          catch
          {
              Console.WriteLine(" Breaking news: a crash occured. ");
          }
      }
      
      private void doFinally()
      {
          Console.WriteLine(" ");
          Console.Write("Here goes: " 
              + (radioReturnInTry.Checked ? "2. Return in try: " 
                      : (radioReturnInCatch.Checked? "3. Retrun in catch: "
                          : (radioThrowInCatch.Checked? "4. Throw in catch: "
                              : "1. Continue in catch: "))) );
          try
          {
              if (radioReturnInTry.Checked)
              {
                  Console.Write(" Returning in try. ");
                  return;
              }
              Console.Write(" Throwing up in try.  ");
              throw new Exception("check your checkbox.");
          }
          catch (Exception ex)
          {
              Console.Write(" ...caughtcha! ");
              if (radioReturnInCatch.Checked)
              {
                  Console.Write("Returning in catch. ");
                  return;
              }
              if (radioThrowInCatch.Checked)
              {
                  Console.Write(" Throwing up in catch. ");
                  throw new Exception("after caught");
              }
          }
          finally { Console.Write(" Finally!!"); }
          Console.WriteLine(" Done!!!"); // before adding checkboxThrowInCatch, 
          // this would never happen (and was marked grey by ReSharper)
      
      }
      

    Output:

    • Here goes: 1. Continue in catch: Throwing up in try. ...caughtcha! Finally!! Done!!!
    • Here goes: 2. Return in try: Returning in try. Finally!!
    • Here goes: 3. Retrun in catch: Throwing up in try. ...caughtcha! Returning in catch. Finally!!
    • Here goes: 4. Throw in catch: Throwing up in try. ...caughtcha! Throwing up in catch. Finally!! Breaking news: a crash occured.

    To summarize: Finally takes care of two things:

    1. Of code that returned in the try or in the catch.
    2. Or If you had an exception in the try, AND THROW an exception in the catch,
    3. or, if you had an exception in the try, AND DID NOT CATCH that exception,

    Finally to summarize "FINALLY": Finally does nothing special if you tried,and

    1. DID NOT RETURN,
    2. and caught any exceptions during the trial, and then
    3. DID NOT RETURN in the catch either, and
    4. DID NOT THROW or have code that throws up.

    And last but not least (finally): If you have an exception in your code that YOU DID NOT CATCH, your code will fly, WITHOUT REACHING THE FINALLY.

    Hope this is clear. (Now it is to me...)

    Moshe

提交回复
热议问题