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. Issue with the below code

Issue with the below code

Scheduled Pinned Locked Moved C#
helpdatabasequestioncode-review
9 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
    Paul McGann
    wrote on last edited by
    #1

    Hi, I was looking for some guidance on best practices regarding error handling.

    public class DemoController : Controller
    {
    private readonly ILogger _logger;

        public DemoController(ILogger logger)
        {
            \_logger = logger;
        }
    
        public ActionResult Index()
        {
            try
            {
                //Do something...
            }
            catch (Exception ex)
            {
                \_logger.Error(ex.ToString());
            }
    
            return View();
        }
    }
    

    I was therefore wondering if anyone would have any suggestions to improve this code? Thanks

    The Dark Knight

    F N B R B 5 Replies Last reply
    0
    • P Paul McGann

      Hi, I was looking for some guidance on best practices regarding error handling.

      public class DemoController : Controller
      {
      private readonly ILogger _logger;

          public DemoController(ILogger logger)
          {
              \_logger = logger;
          }
      
          public ActionResult Index()
          {
              try
              {
                  //Do something...
              }
              catch (Exception ex)
              {
                  \_logger.Error(ex.ToString());
              }
      
              return View();
          }
      }
      

      I was therefore wondering if anyone would have any suggestions to improve this code? Thanks

      The Dark Knight

      F Offline
      F Offline
      F ES Sitecore
      wrote on last edited by
      #2

      Ask the person who told you it was sub-standard what their issues are. They could be referring to the fact that catching just "Exception" is too generic, that you should aim for specific exceptions (that rarely happens in the real world though). Or it could be the fact that you just log the exception and do nothing with it, you go on to return the View as normal so the user has no real knowledge that the code failed.

      P 1 Reply Last reply
      0
      • P Paul McGann

        Hi, I was looking for some guidance on best practices regarding error handling.

        public class DemoController : Controller
        {
        private readonly ILogger _logger;

            public DemoController(ILogger logger)
            {
                \_logger = logger;
            }
        
            public ActionResult Index()
            {
                try
                {
                    //Do something...
                }
                catch (Exception ex)
                {
                    \_logger.Error(ex.ToString());
                }
        
                return View();
            }
        }
        

        I was therefore wondering if anyone would have any suggestions to improve this code? Thanks

        The Dark Knight

        N Offline
        N Offline
        Nathan Minier
        wrote on last edited by
        #3

        It's usually good practice to inform the user that an error occurred, and therefore the output might not be trustworthy or the input was not accepted by the system.

        "There are three kinds of lies: lies, damned lies and statistics." - Benjamin Disraeli

        P 1 Reply Last reply
        0
        • F F ES Sitecore

          Ask the person who told you it was sub-standard what their issues are. They could be referring to the fact that catching just "Exception" is too generic, that you should aim for specific exceptions (that rarely happens in the real world though). Or it could be the fact that you just log the exception and do nothing with it, you go on to return the View as normal so the user has no real knowledge that the code failed.

          P Offline
          P Offline
          Paul McGann
          wrote on last edited by
          #4

          Thanks, makes more sense to me now.

          Paul McGann

          1 Reply Last reply
          0
          • N Nathan Minier

            It's usually good practice to inform the user that an error occurred, and therefore the output might not be trustworthy or the input was not accepted by the system.

            "There are three kinds of lies: lies, damned lies and statistics." - Benjamin Disraeli

            P Offline
            P Offline
            Paul McGann
            wrote on last edited by
            #5

            Thanks Benjamin, i'll keep this in mind for future.

            Paul McGann

            1 Reply Last reply
            0
            • P Paul McGann

              Hi, I was looking for some guidance on best practices regarding error handling.

              public class DemoController : Controller
              {
              private readonly ILogger _logger;

                  public DemoController(ILogger logger)
                  {
                      \_logger = logger;
                  }
              
                  public ActionResult Index()
                  {
                      try
                      {
                          //Do something...
                      }
                      catch (Exception ex)
                      {
                          \_logger.Error(ex.ToString());
                      }
              
                      return View();
                  }
              }
              

              I was therefore wondering if anyone would have any suggestions to improve this code? Thanks

              The Dark Knight

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

              The "best wisdom" I have found re error-handling is in the writings of Eric Lippert, a key .NET architect: his "classic" piece often cited by other language gurus: [^]. Also: [^], [^]. A summary based on Eric's writings: [^]. There's a 3 part series on "Dr. Spock Engineer" blog I think is worth studying that starts here: [^]. I favor always throwing an Exception except in very special cases like within an enumeration of data where partial results are acceptable and usable.

              «Differences between Big-Endians, who broke eggs at the larger end, and Little-Endians gave rise to six rebellions: one Emperor lost his life, another his crown. The Lilliputian religion says an egg should be broken on the convenient end, which is now interpreted by the Lilliputians as the smaller end. Big-Endians gained favor in Blefuscu.» J. Swift, 'Gulliver's Travels,' 1726CE

              P 1 Reply Last reply
              0
              • B BillWoodruff

                The "best wisdom" I have found re error-handling is in the writings of Eric Lippert, a key .NET architect: his "classic" piece often cited by other language gurus: [^]. Also: [^], [^]. A summary based on Eric's writings: [^]. There's a 3 part series on "Dr. Spock Engineer" blog I think is worth studying that starts here: [^]. I favor always throwing an Exception except in very special cases like within an enumeration of data where partial results are acceptable and usable.

                «Differences between Big-Endians, who broke eggs at the larger end, and Little-Endians gave rise to six rebellions: one Emperor lost his life, another his crown. The Lilliputian religion says an egg should be broken on the convenient end, which is now interpreted by the Lilliputians as the smaller end. Big-Endians gained favor in Blefuscu.» J. Swift, 'Gulliver's Travels,' 1726CE

                P Offline
                P Offline
                Paul McGann
                wrote on last edited by
                #7

                Thanks Bill, great articles and advice appreciate everyone's feedback.

                Paul McGann

                1 Reply Last reply
                0
                • P Paul McGann

                  Hi, I was looking for some guidance on best practices regarding error handling.

                  public class DemoController : Controller
                  {
                  private readonly ILogger _logger;

                      public DemoController(ILogger logger)
                      {
                          \_logger = logger;
                      }
                  
                      public ActionResult Index()
                      {
                          try
                          {
                              //Do something...
                          }
                          catch (Exception ex)
                          {
                              \_logger.Error(ex.ToString());
                          }
                  
                          return View();
                      }
                  }
                  

                  I was therefore wondering if anyone would have any suggestions to improve this code? Thanks

                  The Dark Knight

                  R Offline
                  R Offline
                  Ravi Bhavnani
                  wrote on last edited by
                  #8

                  Logging the exception is useful, but I would recommend also throwing (not rethrowing) it.  What I mean is:

                  try {
                  DoSomething();
                  }
                  catch (Exception ex) {
                  this._logger.Log(ex);
                  throw;
                  }

                  /ravi

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

                  1 Reply Last reply
                  0
                  • P Paul McGann

                    Hi, I was looking for some guidance on best practices regarding error handling.

                    public class DemoController : Controller
                    {
                    private readonly ILogger _logger;

                        public DemoController(ILogger logger)
                        {
                            \_logger = logger;
                        }
                    
                        public ActionResult Index()
                        {
                            try
                            {
                                //Do something...
                            }
                            catch (Exception ex)
                            {
                                \_logger.Error(ex.ToString());
                            }
                    
                            return View();
                        }
                    }
                    

                    I was therefore wondering if anyone would have any suggestions to improve this code? Thanks

                    The Dark Knight

                    B Offline
                    B Offline
                    BenScharbach
                    wrote on last edited by
                    #9

                    Yes, instead of writing the same old boiler-code (as they call it), I would use PostSharp to create code-injection attributes which would automatically wrap the method with your try-catch{} exception-logger. This reduces the redundant code and improves the visibility of your class code.

                    Ben Scharbach Temporalwars.Com YouTube:Ben Scharbach

                    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