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. Try, Catch, Finally question

Try, Catch, Finally question

Scheduled Pinned Locked Moved C#
questiontutorial
7 Posts 5 Posters 0 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.
  • J Offline
    J Offline
    JD86
    wrote on last edited by
    #1

    I'm writing some DLL's and have a question. If I throw the exception in the catch block does the finally ever execute? Reason why I am doing this is because the DLL's do not perform logging, the calling class does. But I need to make sure I dispose of my objects. Example:

    SqlConnection conn = null;
    SqlCommand cmd = null;

    try
    {
    }
    catch (Exception ex)
    {
    throw;
    }
    finally
    {
    if (cmd != null)
    cmd.Dispose();

    if (conn != null)
    conn.Dispose();
    }

    IS that proper or should I dispose in the catch block AND the finally block because the finally will never execute since i'm rethrowing the exception?

    A E B 3 Replies Last reply
    0
    • J JD86

      I'm writing some DLL's and have a question. If I throw the exception in the catch block does the finally ever execute? Reason why I am doing this is because the DLL's do not perform logging, the calling class does. But I need to make sure I dispose of my objects. Example:

      SqlConnection conn = null;
      SqlCommand cmd = null;

      try
      {
      }
      catch (Exception ex)
      {
      throw;
      }
      finally
      {
      if (cmd != null)
      cmd.Dispose();

      if (conn != null)
      conn.Dispose();
      }

      IS that proper or should I dispose in the catch block AND the finally block because the finally will never execute since i'm rethrowing the exception?

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      The finally block will almost always execute - let all dispose methods remain in the finally block.

      Apps - Color Analyzer | Arctic | XKCD | Sound Meter | Speed Dial

      J 1 Reply Last reply
      0
      • J JD86

        I'm writing some DLL's and have a question. If I throw the exception in the catch block does the finally ever execute? Reason why I am doing this is because the DLL's do not perform logging, the calling class does. But I need to make sure I dispose of my objects. Example:

        SqlConnection conn = null;
        SqlCommand cmd = null;

        try
        {
        }
        catch (Exception ex)
        {
        throw;
        }
        finally
        {
        if (cmd != null)
        cmd.Dispose();

        if (conn != null)
        conn.Dispose();
        }

        IS that proper or should I dispose in the catch block AND the finally block because the finally will never execute since i'm rethrowing the exception?

        E Offline
        E Offline
        ExcellentOrg
        wrote on last edited by
        #3

        Yes.... code in finally block will always execute unconditionally (unless you shut down the PC by pulling the power cord ...)

        1 Reply Last reply
        0
        • J JD86

          I'm writing some DLL's and have a question. If I throw the exception in the catch block does the finally ever execute? Reason why I am doing this is because the DLL's do not perform logging, the calling class does. But I need to make sure I dispose of my objects. Example:

          SqlConnection conn = null;
          SqlCommand cmd = null;

          try
          {
          }
          catch (Exception ex)
          {
          throw;
          }
          finally
          {
          if (cmd != null)
          cmd.Dispose();

          if (conn != null)
          conn.Dispose();
          }

          IS that proper or should I dispose in the catch block AND the finally block because the finally will never execute since i'm rethrowing the exception?

          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #4

          JD86 wrote:

          make sure I dispose of my objects

          In such a case, I prefer a using statement:

          using (IDbConnection conn = ...)
          {
          using (IDbCommand cmd = ...)
          {
          //do something here
          }
          }

          Rhe Dispose method of the object will be called when the using block is left - regardless of the reason for leaving.

          J 1 Reply Last reply
          0
          • B Bernhard Hiller

            JD86 wrote:

            make sure I dispose of my objects

            In such a case, I prefer a using statement:

            using (IDbConnection conn = ...)
            {
            using (IDbCommand cmd = ...)
            {
            //do something here
            }
            }

            Rhe Dispose method of the object will be called when the using block is left - regardless of the reason for leaving.

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

            I don't really like the using method. No way to really catch errors unless you do another try, catch within the using statement and then that is just redundant. I just stick to try, catch, finally and dispose of everything myself.

            J 1 Reply Last reply
            0
            • A Abhinav S

              The finally block will almost always execute - let all dispose methods remain in the finally block.

              Apps - Color Analyzer | Arctic | XKCD | Sound Meter | Speed Dial

              J Offline
              J Offline
              JD86
              wrote on last edited by
              #6

              Thanks! That is what I needed to know! Just wasn't 100% if the finally would execute because I threw the error again in the catch block.

              1 Reply Last reply
              0
              • J JD86

                I don't really like the using method. No way to really catch errors unless you do another try, catch within the using statement and then that is just redundant. I just stick to try, catch, finally and dispose of everything myself.

                J Offline
                J Offline
                Jean A Brandelero
                wrote on last edited by
                #7

                So use finally, it will do exactly you want. Just to reinforce, yes, it runs all the time, in good or bad cases (exceptions).

                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