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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Automatic insertion of try catch block

Automatic insertion of try catch block

Scheduled Pinned Locked Moved C#
18 Posts 10 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.
  • T thomforum

    Calla -- Yup I am using VS 2005. But I want it automated. How will I trigger it ? cheers

    F Offline
    F Offline
    fjdiewornncalwe
    wrote on last edited by
    #4

    You may wish to ask yourself why you want to insert a try-catch into every method you are creating. That is no way to trap errors properly. It will just make the entire application nearly impossible to effectively debug the application. If you do wish to do it, look up on how to creating studio add-ins. You may want to tie in that way to automate the process with no user intervention(i.e. keystrokes), but the logical way would be to create a snippet, and assign a keystroke to it so that you only have to type that to get the whole block inserted.

    I wasn't, now I am, then I won't be anymore.

    1 Reply Last reply
    0
    • T thomforum

      Calla -- Yup I am using VS 2005. But I want it automated. How will I trigger it ? cheers

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #5

      I'm with PogoboyKramer on this - try catch round each and every method just look like you are saying "I don't know what I am doing, so I'll try and ignore as many problems as I can instead of fixing the problems behind them" Smacks to me of VB's "On Error Resume Next" X|

      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      F 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        I'm with PogoboyKramer on this - try catch round each and every method just look like you are saying "I don't know what I am doing, so I'll try and ignore as many problems as I can instead of fixing the problems behind them" Smacks to me of VB's "On Error Resume Next" X|

        Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

        F Offline
        F Offline
        fjdiewornncalwe
        wrote on last edited by
        #6

        OriginalGriff wrote:

        On Error Resume Next

        Goto Happyland

        I wasn't, now I am, then I won't be anymore.

        OriginalGriffO 1 Reply Last reply
        0
        • F fjdiewornncalwe

          OriginalGriff wrote:

          On Error Resume Next

          Goto Happyland

          I wasn't, now I am, then I won't be anymore.

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #7

          PogoboyKramer wrote:

          Goto VeryUnHappyland if you are the poor sod maintaining it...

          FTFY! :laugh:

          Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          F 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            PogoboyKramer wrote:

            Goto VeryUnHappyland if you are the poor sod maintaining it...

            FTFY! :laugh:

            Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

            F Offline
            F Offline
            fjdiewornncalwe
            wrote on last edited by
            #8

            Reading between the lines... You've worked with BA's before

            I wasn't, now I am, then I won't be anymore.

            OriginalGriffO 1 Reply Last reply
            0
            • F fjdiewornncalwe

              Reading between the lines... You've worked with BA's before

              I wasn't, now I am, then I won't be anymore.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #9

              Who hasn't? :laugh:

              Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • T thomforum

                Dear All -- Forgive me if I am being greedy. Upon writing a function header say "Private void FntName()" is there a way the two curly braces and the try catch block is automatically inserted. :) cheers

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #10

                If you like removing said Try/Catch blocks all the time, this would be a good idea. But, it's a horrible idea to put a try/catch block in every method. You're not really doing/handling exceptions properly if this is the case, but instead making a nightmare of debugging your app.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak

                T 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  If you like removing said Try/Catch blocks all the time, this would be a good idea. But, it's a horrible idea to put a try/catch block in every method. You're not really doing/handling exceptions properly if this is the case, but instead making a nightmare of debugging your app.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak

                  T Offline
                  T Offline
                  thomforum
                  wrote on last edited by
                  #11

                  Dave -- Tell me one instance where you wouldn't prefer placing a try/catch in a method. cheers

                  J P D 3 Replies Last reply
                  0
                  • T thomforum

                    Dave -- Tell me one instance where you wouldn't prefer placing a try/catch in a method. cheers

                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #12

                    thomforum wrote:

                    Tell me one instance where you wouldn't prefer placing a try/catch in a method.

                    Two instances. There are many others.

                    public int Count
                    {
                    get { return count; }
                    set
                    {
                    if (value >= 0)
                    count = value;
                    }
                    }
                    private int count;

                    public void Initialize(String name)
                    {
                    if (String.IsNullOrEmpty(name))
                    throw new ArgumentException("Name must be defined");
                    this.name = name;
                    }

                    I also do not want to see the following. The following might require try/catch somewhere but logging the exception twice does nothing but provide confusion. So the question becomes what does the one try/catch do that the other does not?

                    private void ProcessTxn()
                    {
                    try
                    {
                    ...
                    }
                    catch(Exception e)
                    {
                    __log.Error("Failed to process txn", e);
                    throw;
                    }
                    }

                    public void Process()
                    {
                    try
                    {
                    PrepareTxn();
                    ProcessTxn();
                    SaveTxn();
                    }
                    catch(Exception e)
                    {
                    __log.Error("Failed to process", e);
                    throw;
                    }
                    }

                    F 1 Reply Last reply
                    0
                    • J jschell

                      thomforum wrote:

                      Tell me one instance where you wouldn't prefer placing a try/catch in a method.

                      Two instances. There are many others.

                      public int Count
                      {
                      get { return count; }
                      set
                      {
                      if (value >= 0)
                      count = value;
                      }
                      }
                      private int count;

                      public void Initialize(String name)
                      {
                      if (String.IsNullOrEmpty(name))
                      throw new ArgumentException("Name must be defined");
                      this.name = name;
                      }

                      I also do not want to see the following. The following might require try/catch somewhere but logging the exception twice does nothing but provide confusion. So the question becomes what does the one try/catch do that the other does not?

                      private void ProcessTxn()
                      {
                      try
                      {
                      ...
                      }
                      catch(Exception e)
                      {
                      __log.Error("Failed to process txn", e);
                      throw;
                      }
                      }

                      public void Process()
                      {
                      try
                      {
                      PrepareTxn();
                      ProcessTxn();
                      SaveTxn();
                      }
                      catch(Exception e)
                      {
                      __log.Error("Failed to process", e);
                      throw;
                      }
                      }

                      F Offline
                      F Offline
                      fjdiewornncalwe
                      wrote on last edited by
                      #13

                      I think the discussion isn't about the inherent value of a try-catch block. It is the problems associated with simply making an exception go away with no logging or anything in an empty try catch.

                      I wasn't, now I am, then I won't be anymore.

                      1 Reply Last reply
                      0
                      • T thomforum

                        Dear All -- Forgive me if I am being greedy. Upon writing a function header say "Private void FntName()" is there a way the two curly braces and the try catch block is automatically inserted. :) cheers

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #14

                        You'd want to handle the ThreadException[^]. If you want to swallow exceptions, do it there - after logging them or something like that. There's an example on the page on setting up basic exception-handling.

                        I are Troll :suss:

                        1 Reply Last reply
                        0
                        • T thomforum

                          Dave -- Tell me one instance where you wouldn't prefer placing a try/catch in a method. cheers

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

                          You should only use a try/catch if you plan to do something with the exception. Exception handling is intended to provide a defense mechanism, where you can actually handle the exception - what's the point of just consuming it? If you just catch the exception then you don't know that part of your code encountered a problem, so you end up masking problems from a user. As an example, what happens if you have a network connection open and connectivity is lost? If you are writing to the network stream, it's a good idea to know that it's closed unexpectedly during a stream write, so you can inform the user that the write didn't complete successfully.

                          thomforum wrote:

                          Tell me one instance where you wouldn't prefer placing a try/catch in a method.

                          This, effectively, is blind optimism. Use exception handling for what it's intended for.

                          I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

                          Forgive your enemies - it messes with their heads

                          My blog | My articles | MoXAML PowerToys | Onyx

                          1 Reply Last reply
                          0
                          • T thomforum

                            Dave -- Tell me one instance where you wouldn't prefer placing a try/catch in a method. cheers

                            D Offline
                            D Offline
                            Dave Kreskowiak
                            wrote on last edited by
                            #16

                            When I want the exception to bubble up to a higher level to be handled at a business process level where I can centralize problem logging and abort whatever transaction is going on at the time, which is most of the time. Doing it your way would limit the exception to that function, where you'd have to throw a new exception to get it to go higher. But, don't take my word for it. There's tons of articles on the subject: Google Results[^]

                            A guide to posting questions on CodeProject[^]
                            Dave Kreskowiak

                            1 Reply Last reply
                            0
                            • T thomforum

                              Dear All -- Forgive me if I am being greedy. Upon writing a function header say "Private void FntName()" is there a way the two curly braces and the try catch block is automatically inserted. :) cheers

                              RaviBeeR Offline
                              RaviBeeR Offline
                              RaviBee
                              wrote on last edited by
                              #17

                              Yes. See this[^] code snippet. /ravi

                              My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                              1 Reply Last reply
                              0
                              • T thomforum

                                Dear All -- Forgive me if I am being greedy. Upon writing a function header say "Private void FntName()" is there a way the two curly braces and the try catch block is automatically inserted. :) cheers

                                A Offline
                                A Offline
                                abinmaths
                                wrote on last edited by
                                #18

                                to add try catch block u type try and give two tab entry using key board u will get the try catch block automaticly

                                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