Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Return statement in try block

Return statement in try block

Scheduled Pinned Locked Moved C#
question
6 Posts 5 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Weiye Chen
    wrote on last edited by
    #1

    Refering below, will the code in finally block be executed even if a return is called in the try block?

    void Somefunc()
    {
    try {
    .
    .
    return;
    }
    catch(Exception) {
    .
    .
    }
    finally {
    .
    SomeCode();
    }
    }

    Weiye Chen Give me the Death Note, and I'll cleanse the world...

    B J P 3 Replies Last reply
    0
    • W Weiye Chen

      Refering below, will the code in finally block be executed even if a return is called in the try block?

      void Somefunc()
      {
      try {
      .
      .
      return;
      }
      catch(Exception) {
      .
      .
      }
      finally {
      .
      SomeCode();
      }
      }

      Weiye Chen Give me the Death Note, and I'll cleanse the world...

      B Offline
      B Offline
      Bhupi Bhai
      wrote on last edited by
      #2

      hey!! ru testing us ;-) Regards, Bhupi Bhai.

      W Q 2 Replies Last reply
      0
      • B Bhupi Bhai

        hey!! ru testing us ;-) Regards, Bhupi Bhai.

        W Offline
        W Offline
        Weiye Chen
        wrote on last edited by
        #3

        Bhupi Bhai wrote:

        ru testing us

        :doh: I dont think i would be so free to do such things. Anyway i was trying to confirm if finally would be called, but i seem to forgot that i could easiy test it out. :doh: Regardless i saw a link[^] about this. Just for anyone who is interested.

        Weiye Chen Give me the Death Note, and I'll cleanse the world...

        1 Reply Last reply
        0
        • B Bhupi Bhai

          hey!! ru testing us ;-) Regards, Bhupi Bhai.

          Q Offline
          Q Offline
          quiteSmart
          wrote on last edited by
          #4

          hahhahaa :):) :laugh: :laugh: nice one but you should answer him cz maybe he is knew on C#. The answer is that the finally clause is executed in all the senarios good luck

          1 Reply Last reply
          0
          • W Weiye Chen

            Refering below, will the code in finally block be executed even if a return is called in the try block?

            void Somefunc()
            {
            try {
            .
            .
            return;
            }
            catch(Exception) {
            .
            .
            }
            finally {
            .
            SomeCode();
            }
            }

            Weiye Chen Give me the Death Note, and I'll cleanse the world...

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            Yes, it is.

            --- How to get answers to your questions[^]

            1 Reply Last reply
            0
            • W Weiye Chen

              Refering below, will the code in finally block be executed even if a return is called in the try block?

              void Somefunc()
              {
              try {
              .
              .
              return;
              }
              catch(Exception) {
              .
              .
              }
              finally {
              .
              SomeCode();
              }
              }

              Weiye Chen Give me the Death Note, and I'll cleanse the world...

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              It is. The purpose of a finally block is to guarantee that a piece of code will be run, regardless of what else happens in the try catch block. This gives you the ability to guarantee that cleanup code will be carried out.

              the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
              Deja View - the feeling that you've seen this post before.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups