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. Is there a extended TryCatch-Snippet ?

Is there a extended TryCatch-Snippet ?

Scheduled Pinned Locked Moved C#
question
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.
  • M Offline
    M Offline
    MarkPhB
    wrote on last edited by
    #1

    Hi, Does anyone know whether there is a TryCatch-Snippet wich can built try{}catch{}-blocks with all possible Exceptions thrown by a Methode ? or Is there a way to create such a snippet ?

    G N J 3 Replies Last reply
    0
    • M MarkPhB

      Hi, Does anyone know whether there is a TryCatch-Snippet wich can built try{}catch{}-blocks with all possible Exceptions thrown by a Methode ? or Is there a way to create such a snippet ?

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      I don't know if such snippets exist but I know that you can create your own snippets

      my articles

      1 Reply Last reply
      0
      • M MarkPhB

        Hi, Does anyone know whether there is a TryCatch-Snippet wich can built try{}catch{}-blocks with all possible Exceptions thrown by a Methode ? or Is there a way to create such a snippet ?

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        There isn't one. How would it be possible for the snippet manager to know all Exceptions possible from a method, especially if the method made calls other methods in an external assembly?


        only two letters away from being an asset

        1 Reply Last reply
        0
        • M MarkPhB

          Hi, Does anyone know whether there is a TryCatch-Snippet wich can built try{}catch{}-blocks with all possible Exceptions thrown by a Methode ? or Is there a way to create such a snippet ?

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          try
          {
          }
          catch(Exception error)
          {
          }

          will catch all exceptions. However, this is not a recommended practice since it will also catch unrecoverable or systems exceptions (e.g. System.OutOfMemoryException, SecurityException, ThreadAbortException, etc.) The best practice is to catch only the exceptions you can recover from. Otherwise, don't handle them.

          Tech, life, family, faith: Give me a visit. I'm currently blogging about: Master's Decree: It's By My Spirit (audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

          G 1 Reply Last reply
          0
          • J Judah Gabriel Himango

            try
            {
            }
            catch(Exception error)
            {
            }

            will catch all exceptions. However, this is not a recommended practice since it will also catch unrecoverable or systems exceptions (e.g. System.OutOfMemoryException, SecurityException, ThreadAbortException, etc.) The best practice is to catch only the exceptions you can recover from. Otherwise, don't handle them.

            Tech, life, family, faith: Give me a visit. I'm currently blogging about: Master's Decree: It's By My Spirit (audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            Judah Himango wrote:

            will catch all exceptions

            Well, not all, only .NET exceptions. To catch all exceptions, you have to specify a parameterless catch:

            try {
            // code
            } catch {
            // catches any exception
            }

            --- single minded; short sighted; long gone;

            J K 2 Replies Last reply
            0
            • G Guffa

              Judah Himango wrote:

              will catch all exceptions

              Well, not all, only .NET exceptions. To catch all exceptions, you have to specify a parameterless catch:

              try {
              // code
              } catch {
              // catches any exception
              }

              --- single minded; short sighted; long gone;

              J Offline
              J Offline
              Judah Gabriel Himango
              wrote on last edited by
              #6

              Right, it will catch all .NET exceptions. An empty catch will catch non-exception objects -- CLI languages are urged, but not required to, throw only System.Exception objects. However, I wouldn't call those exceptions anymore; those are thrown objects, not thrown exceptions, right?

              Tech, life, family, faith: Give me a visit. I'm currently blogging about: Master's Decree: It's By My Spirit (audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

              G 1 Reply Last reply
              0
              • G Guffa

                Judah Himango wrote:

                will catch all exceptions

                Well, not all, only .NET exceptions. To catch all exceptions, you have to specify a parameterless catch:

                try {
                // code
                } catch {
                // catches any exception
                }

                --- single minded; short sighted; long gone;

                K Offline
                K Offline
                Kevin McFarlane
                wrote on last edited by
                #7

                I read somewhere that this has changed in .NET 2.0 and you don't need empty catch blocks anymore.

                Kevin

                J 1 Reply Last reply
                0
                • J Judah Gabriel Himango

                  Right, it will catch all .NET exceptions. An empty catch will catch non-exception objects -- CLI languages are urged, but not required to, throw only System.Exception objects. However, I wouldn't call those exceptions anymore; those are thrown objects, not thrown exceptions, right?

                  Tech, life, family, faith: Give me a visit. I'm currently blogging about: Master's Decree: It's By My Spirit (audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  Judah Himango wrote:

                  However, I wouldn't call those exceptions anymore; those are thrown objects, not thrown exceptions, right?

                  I would call an exception an exception, regardless of what object is thrown to handle it. The exception is the situation, not the object.

                  --- single minded; short sighted; long gone;

                  J 1 Reply Last reply
                  0
                  • G Guffa

                    Judah Himango wrote:

                    However, I wouldn't call those exceptions anymore; those are thrown objects, not thrown exceptions, right?

                    I would call an exception an exception, regardless of what object is thrown to handle it. The exception is the situation, not the object.

                    --- single minded; short sighted; long gone;

                    J Offline
                    J Offline
                    Judah Gabriel Himango
                    wrote on last edited by
                    #9

                    Ah, see, when I was talking about exceptions, I was talking about System.Exception-based classes. Anyways, I suppose it's not such a big deal. *edit* after looking into this a bit, I see the framework also refers to non-System.Exception objects which are thrown as "exceptions" in the documentation - interesting! So, you were right. Another interesting side note is the System.Runtime.CompilerServices.RuntimeCompatibilityAttribute which tells the CLR to wrap all non-System.Exception "exceptions" in a real exception class before throwing it.

                    Tech, life, family, faith: Give me a visit. I'm currently blogging about: Master's Decree: It's By My Spirit (audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

                    1 Reply Last reply
                    0
                    • K Kevin McFarlane

                      I read somewhere that this has changed in .NET 2.0 and you don't need empty catch blocks anymore.

                      Kevin

                      J Offline
                      J Offline
                      Judah Gabriel Himango
                      wrote on last edited by
                      #10

                      There is a System.Runtime.CompilerServices.RuntimeCompatibilityAttribute for assemblies that tells the runtime whether to wrap all "exceptions" that do not derive from System.Exception with a System.Runtime.CompilerServices.RuntimeWrappedException. Maybe that's what you're thinking of?

                      Tech, life, family, faith: Give me a visit. I'm currently blogging about: Master's Decree: It's By My Spirit (audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

                      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