[C#] Is it possible to implicitly wrap each line in a try/catch block?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Logan1337
    New Member
    • May 2007
    • 38

    [C#] Is it possible to implicitly wrap each line in a try/catch block?

    Hi all. I have a method involving several statements, none of which are dependent on any other. Basically I am simply setting a bunch of properties, one after the other, and want to essentially ignore exceptions on a per-line basis throughout this block. In other words, if an exception occurs, I only want that line to be skipped and execution continue on the next, just as if I had this:

    Code:
    void method() {
        try { NonCriticalOperation(); } catch { }
        try { NonCriticalOperation(); } catch { }
        try { NonCriticalOperation(); } catch { }
        try { NonCriticalOperation(); } catch { }
        try { NonCriticalOperation(); } catch { }
    }
    ...but without having to write a million try/catch statements.

    Anyone know of an attribute or keyword that can do this?
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Originally posted by Logan1337
    Hi all. I have a method involving several statements, none of which are dependent on any other. Basically I am simply setting a bunch of properties, one after the other, and want to essentially ignore exceptions on a per-line basis throughout this block. In other words, if an exception occurs, I only want that line to be skipped and execution continue on the next, just as if I had this:

    Code:
    void method() {
        try { NonCriticalOperation(); } catch { }
        try { NonCriticalOperation(); } catch { }
        try { NonCriticalOperation(); } catch { }
        try { NonCriticalOperation(); } catch { }
        try { NonCriticalOperation(); } catch { }
    }
    ...but without having to write a million try/catch statements.

    Anyone know of an attribute or keyword that can do this?
    I have no clue about that.
    But if you can write custom snippets it will help (or try editing the try snippet and save it as a new one)

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      I don't believe it's possible...UNLE SS you can manage to do something like a loop for property setting

      Psuedo:
      Code:
      foreach(property myprop in PropertiesToSet)
      {
         try
         {
            SomeFunctionToSetProperty(myprop);
         }
         catch(Exception e)
         {
            //ignore errors
         }
      }

      Comment

      • TRScheel
        Recognized Expert Contributor
        • Apr 2007
        • 638

        #4
        You may want to read:

        Why Doesnt C# Have Exception Specifications?


        On a side note, its generally bad practice to just ignore exceptions. Granted, there are times when its warranted, but I would seriously consider if there was another way to go about it.

        Comment

        Working...