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. Best approach to use try catch finally

Best approach to use try catch finally

Scheduled Pinned Locked Moved C#
designquestion
10 Posts 6 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.
  • P Offline
    P Offline
    Pankaj Saha
    wrote on last edited by
    #1

    Hi, I created a class library and wrote a function with the try {} catch { throw ;} finally {} manner. However I am not doing anything in the catch block, only throwing the exception. I am handling the exception the UI layer which is using that class library. I want to know the best approach to write the try block. If I use only try {} finally {} without catch block, does it good ?:confused:

    Pankaj

    D A P L 4 Replies Last reply
    0
    • P Pankaj Saha

      Hi, I created a class library and wrote a function with the try {} catch { throw ;} finally {} manner. However I am not doing anything in the catch block, only throwing the exception. I am handling the exception the UI layer which is using that class library. I want to know the best approach to write the try block. If I use only try {} finally {} without catch block, does it good ?:confused:

      Pankaj

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      Are you even allowed to skip catch? I dont think so. If the method calling the Class library method handles the exceptions(even for the class library methods), you can avoid try-catch block in the class library method.

      P 1 Reply Last reply
      0
      • D dan sh

        Are you even allowed to skip catch? I dont think so. If the method calling the Class library method handles the exceptions(even for the class library methods), you can avoid try-catch block in the class library method.

        P Offline
        P Offline
        Pankaj Saha
        wrote on last edited by
        #3

        I can not avoid the try catch finally in my class library. Because I am doing file operation in the class library, that's why I have to open and working on the files and in the finally block I am closing the writer/reader/file.

        Pankaj

        1 Reply Last reply
        0
        • P Pankaj Saha

          Hi, I created a class library and wrote a function with the try {} catch { throw ;} finally {} manner. However I am not doing anything in the catch block, only throwing the exception. I am handling the exception the UI layer which is using that class library. I want to know the best approach to write the try block. If I use only try {} finally {} without catch block, does it good ?:confused:

          Pankaj

          A Offline
          A Offline
          Alan N
          wrote on last edited by
          #4

          Hi, From the C# 2.0 standard: There are three possible forms of try statements: • A try block followed by one or more catch blocks. • A try block followed by a finally block. • A try block followed by one or more catch blocks followed by a finally block. Alan.

          1 Reply Last reply
          0
          • P Pankaj Saha

            Hi, I created a class library and wrote a function with the try {} catch { throw ;} finally {} manner. However I am not doing anything in the catch block, only throwing the exception. I am handling the exception the UI layer which is using that class library. I want to know the best approach to write the try block. If I use only try {} finally {} without catch block, does it good ?:confused:

            Pankaj

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

            You can use try/finally without the catch, but you have to think about why you are doing this. As you're not catching exceptions at the level that they are being thrown, you are storing up a lot of work for the higher levels. By this, I mean that you could catch the exception and do something meaningful with it, rather than relying on the UI reporting that there was an error. For instance, we have a library that catches an error where the SqlConnection has become unavailable, and (in the appropriate catch), reconnects the SqlConnection before it attempts to process the SQL again.

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

            My blog | My articles | MoXAML PowerToys | Onyx

            P 1 Reply Last reply
            0
            • P Pete OHanlon

              You can use try/finally without the catch, but you have to think about why you are doing this. As you're not catching exceptions at the level that they are being thrown, you are storing up a lot of work for the higher levels. By this, I mean that you could catch the exception and do something meaningful with it, rather than relying on the UI reporting that there was an error. For instance, we have a library that catches an error where the SqlConnection has become unavailable, and (in the appropriate catch), reconnects the SqlConnection before it attempts to process the SQL again.

              "WPF has many lovers. It's a veritable porn star!" - Josh Smith

              As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

              My blog | My articles | MoXAML PowerToys | Onyx

              P Offline
              P Offline
              Pankaj Saha
              wrote on last edited by
              #6

              I do not want to use the try catch finally in the class library, however I am doing file operations in the class library. I open/read/write the file with the stream reader/writer object and have to close that reader/writer in the finally block. If any error occurs during the read/write operation then reader must be close, that's why I have to use the finally block to close the reader. Here is the code public string ReadXmlNodeValue(string fileName, string xmlPath) { string retValue = ""; StreamReader sr = null; XmlDocument doc = new XmlDocument(); try { sr = new StreamReader(fileName, Encoding.UTF8); doc.Load(sr); XmlNode element = doc.SelectSingleNode(xmlPath); retValue = element.InnerText; } catch { throw; } finally { sr.Close(); sr = null; doc = null; } return retValue; } Can I write the above code without try catch finally block and it will close the reader if any error is generated during the process ?

              Pankaj

              P 1 Reply Last reply
              0
              • P Pankaj Saha

                I do not want to use the try catch finally in the class library, however I am doing file operations in the class library. I open/read/write the file with the stream reader/writer object and have to close that reader/writer in the finally block. If any error occurs during the read/write operation then reader must be close, that's why I have to use the finally block to close the reader. Here is the code public string ReadXmlNodeValue(string fileName, string xmlPath) { string retValue = ""; StreamReader sr = null; XmlDocument doc = new XmlDocument(); try { sr = new StreamReader(fileName, Encoding.UTF8); doc.Load(sr); XmlNode element = doc.SelectSingleNode(xmlPath); retValue = element.InnerText; } catch { throw; } finally { sr.Close(); sr = null; doc = null; } return retValue; } Can I write the above code without try catch finally block and it will close the reader if any error is generated during the process ?

                Pankaj

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

                Pankaj Saha wrote:

                Can I write the above code without try catch finally block and it will close the reader if any error is generated during the process ?

                If you mean, can you dispense with try/finally as well, then you should not. The finally portion will close the reader, regardless as to whether or not an exception is thrown. As StreamReader is a disposable object though, you could always rewrite this with:

                using(StreamReader sr = new StreamReader(fileName, Encoding.UTF8))
                {
                // Do the file stuff.
                }

                When StreamReader is disposed the stream is closed (if it is open).

                "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                My blog | My articles | MoXAML PowerToys | Onyx

                1 Reply Last reply
                0
                • P Pankaj Saha

                  Hi, I created a class library and wrote a function with the try {} catch { throw ;} finally {} manner. However I am not doing anything in the catch block, only throwing the exception. I am handling the exception the UI layer which is using that class library. I want to know the best approach to write the try block. If I use only try {} finally {} without catch block, does it good ?:confused:

                  Pankaj

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  Hi, a try block accepts one or more catch blocks and one or no finally blocks; When there is a finally block, the catch blocks can be left out. A catch block that only throws does not make any sense; just throw it out! :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                  P 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Hi, a try block accepts one or more catch blocks and one or no finally blocks; When there is a finally block, the catch blocks can be left out. A catch block that only throws does not make any sense; just throw it out! :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    Exactly.

                    L 1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Exactly.

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      :beer:

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                      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