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. error handling

error handling

Scheduled Pinned Locked Moved C#
csharpvisual-studiohelpquestion
10 Posts 8 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.
  • D Offline
    D Offline
    dcof
    wrote on last edited by
    #1

    In a C#.net 2010 application that I am suppose to maintain (since I am new to working with this language), I find code that looks like the following: [^]try { //do something} catch (ex){} [^] In the visual studio ide, I see a a curly green under the ex. I also do not see any using statement that I would think applies. Thus can you tell me if this kind of code really catches any errors? if so can you explain it to me? I put this kind of code around some of my new code and everything falls into the catch block. Thus can you tell me what a "good" try catch block should look like aand what namespace should be included in the using statement?

    L A V 3 Replies Last reply
    0
    • D dcof

      In a C#.net 2010 application that I am suppose to maintain (since I am new to working with this language), I find code that looks like the following: [^]try { //do something} catch (ex){} [^] In the visual studio ide, I see a a curly green under the ex. I also do not see any using statement that I would think applies. Thus can you tell me if this kind of code really catches any errors? if so can you explain it to me? I put this kind of code around some of my new code and everything falls into the catch block. Thus can you tell me what a "good" try catch block should look like aand what namespace should be included in the using statement?

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

      0. your example is pretty bad, e.g. it is missing the type of the exception you're catching. 1. the green curly indicates the variable "ex" isn't used anywhere. If you aren't going to use it, leave out the variable's name, so write:

      ...
      catch(FileNotFoundException) {
      log("Saving the customer data failed because the necessary file wasn't there");
      }

      2. a good try-catch construct would have one or more regular statements in the try-block, and at least one in the catch-block. 3. an empty catch block most often is very bad (it "swallows" the error information without leaving a trace). One should only catch things one can deal with, and it is always wise to log the exception. 4. There is no connection between try-catch and using. 5. And of course the C# reference material has told you you can have 0 or more catch-blocks and 0 or 1 finally-block; all with good examples in the doc. :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      W D 2 Replies Last reply
      0
      • D dcof

        In a C#.net 2010 application that I am suppose to maintain (since I am new to working with this language), I find code that looks like the following: [^]try { //do something} catch (ex){} [^] In the visual studio ide, I see a a curly green under the ex. I also do not see any using statement that I would think applies. Thus can you tell me if this kind of code really catches any errors? if so can you explain it to me? I put this kind of code around some of my new code and everything falls into the catch block. Thus can you tell me what a "good" try catch block should look like aand what namespace should be included in the using statement?

        A Offline
        A Offline
        AditSheth
        wrote on last edited by
        #3

        dcof wrote:

        catch (ex){}

        You have to specify wich type of exception may handle by this try-catch. List of Exception

        catch (Exception ex){}

        dcof wrote:

        curly green   under the ex.

        Curly green indicate that variable is declared but not used inside catch block.So throw exception.

        catch (Exception ex)
        {
        throw ex;
        }

        P 1 Reply Last reply
        0
        • L Luc Pattyn

          0. your example is pretty bad, e.g. it is missing the type of the exception you're catching. 1. the green curly indicates the variable "ex" isn't used anywhere. If you aren't going to use it, leave out the variable's name, so write:

          ...
          catch(FileNotFoundException) {
          log("Saving the customer data failed because the necessary file wasn't there");
          }

          2. a good try-catch construct would have one or more regular statements in the try-block, and at least one in the catch-block. 3. an empty catch block most often is very bad (it "swallows" the error information without leaving a trace). One should only catch things one can deal with, and it is always wise to log the exception. 4. There is no connection between try-catch and using. 5. And of course the C# reference material has told you you can have 0 or more catch-blocks and 0 or 1 finally-block; all with good examples in the doc. :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          W Offline
          W Offline
          Wayne Gaylard
          wrote on last edited by
          #4

          Luc Pattyn wrote:

          And of course the C# reference material has told you

          But of course... :laugh:

          When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

          1 Reply Last reply
          0
          • A AditSheth

            dcof wrote:

            catch (ex){}

            You have to specify wich type of exception may handle by this try-catch. List of Exception

            catch (Exception ex){}

            dcof wrote:

            curly green   under the ex.

            Curly green indicate that variable is declared but not used inside catch block.So throw exception.

            catch (Exception ex)
            {
            throw ex;
            }

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

            Your advice to just throw the exception isn't the best thing you can do. You should never catch an exception and rethrow it without doing something else with the exception. The reason for this is because all you have done is recreate what would happen if you didn't have any exception handling at all.

            Forgive your enemies - it messes with their heads

            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

            My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

            B 1 Reply Last reply
            0
            • P Pete OHanlon

              Your advice to just throw the exception isn't the best thing you can do. You should never catch an exception and rethrow it without doing something else with the exception. The reason for this is because all you have done is recreate what would happen if you didn't have any exception handling at all.

              Forgive your enemies - it messes with their heads

              "Mind bleach! Send me mind bleach!" - Nagy Vilmos

              My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

              B Offline
              B Offline
              BobJanova
              wrote on last edited by
              #6

              All you've done is get back to the same situation plus paid the cost of throwing another exception! So yeah, don't do that.

              1 Reply Last reply
              0
              • D dcof

                In a C#.net 2010 application that I am suppose to maintain (since I am new to working with this language), I find code that looks like the following: [^]try { //do something} catch (ex){} [^] In the visual studio ide, I see a a curly green under the ex. I also do not see any using statement that I would think applies. Thus can you tell me if this kind of code really catches any errors? if so can you explain it to me? I put this kind of code around some of my new code and everything falls into the catch block. Thus can you tell me what a "good" try catch block should look like aand what namespace should be included in the using statement?

                V Offline
                V Offline
                V 0
                wrote on last edited by
                #7

                Luc gave you a very good answer. Here's a quick simple example, but try the MSDN site or maybe the articles here on CP for proper exception handling.

                try{
                // code here
                }
                catch(Exception ex){
                //Note that ex, in contrary of your example is initialized as the Exception type.
                //Your code would work in javascript for example, but in C# you need to always define the type of a variable
                // the very least you need to do is LOG the error somewhere. Note that Exception is a general class,
                //you can also catch specific Exception like IndexOutOfBoundsException, OutOfMemoryException, ...
                //You can use multiple catch blocks if you wich and handle logging, user information, ... accordingly
                //But I believe that if you catch you should at the very least catch the Exception
                }
                finally{
                //Mostly the finally block is used the handle things that must be done even if the catch block was called.
                //eg. A rollback against the database, close a database connection or close a file handle.
                }

                tip: Do not show technical Exception messages to the user. Instead, depending on the catch, form a nice message explaining what happened and if they need to take action. Hope this helps.

                V.

                1 Reply Last reply
                0
                • L Luc Pattyn

                  0. your example is pretty bad, e.g. it is missing the type of the exception you're catching. 1. the green curly indicates the variable "ex" isn't used anywhere. If you aren't going to use it, leave out the variable's name, so write:

                  ...
                  catch(FileNotFoundException) {
                  log("Saving the customer data failed because the necessary file wasn't there");
                  }

                  2. a good try-catch construct would have one or more regular statements in the try-block, and at least one in the catch-block. 3. an empty catch block most often is very bad (it "swallows" the error information without leaving a trace). One should only catch things one can deal with, and it is always wise to log the exception. 4. There is no connection between try-catch and using. 5. And of course the C# reference material has told you you can have 0 or more catch-blocks and 0 or 1 finally-block; all with good examples in the doc. :)

                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                  D Offline
                  D Offline
                  dcof
                  wrote on last edited by
                  #8

                  When I was talking about the using statement, I was wanting to know what namespace should be used.

                  F L 2 Replies Last reply
                  0
                  • D dcof

                    When I was talking about the using statement, I was wanting to know what namespace should be used.

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

                    "Exception" is part of the System namespace. (ie. System.Exception)

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

                    1 Reply Last reply
                    0
                    • D dcof

                      When I was talking about the using statement, I was wanting to know what namespace should be used.

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

                      Assuming you have a typical set of usings (Visual always gives you using.System to start with), there is nothing you have to add. :)

                      Luc Pattyn [My Articles] Nil Volentibus Arduum

                      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